请仔细阅读下面的代码。String[] Info
包含2个这样的值:shiftDirection & 1
或currGear & 5
和类似的对。
如果收到shiftDirection & 0
,则应显示以前收到的currGear
值。
因此,我将PresentGear
创建为全局变量,并在收到currGear
时将其值存储在PresentGear
中。
然后,当我收到shiftDirection & 0
时,我尝试显示PresentGear
,但是什么也没有显示。PresentGear
未分配任何值。如果有人可以建议为什么会发生这种情况,或者如果我的方法是错误的,请提出另一种方法,这将非常有帮助。
干杯,
马杜
public class Images extends Activity {
/** Called when the activity is first created. */
//LinearLayout mLinearLayout;
//ImageView showImage;
String PresentGear = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
String[] Info = extras.getStringArray("MsgInfo");
Log.d("TCP","in DA Images. Message name: " + Info[0] + ", value: " + Info[1]);
if (Info[0].equals("currGear")) {
PresentGear = Info[1];
setContentView(R.layout.image);
TextView text_bottom = (TextView) findViewById(R.id.textView2);
text_bottom.setText(Info[1]);
}
Log.d("TCP","in DA Images. Present gear1: " + PresentGear);
DataAction(Info[0], Info[1]);
}
public void DataAction (String mName, String mVal) {
String _mName = mName;
String _mVal = mVal;
if (_mName.equals("shiftDirection") && _mVal.equals("1")) {
setContentView(R.layout.image);
//TextView text_top = (TextView) findViewById(R.id.textView1);
ImageView showImage = (ImageView) findViewById(R.id.imageView1);
//text_bottom.setText(Info[1]);
showImage.setImageResource(R.drawable.shift_up);
} else if (_mName.equals("shiftDirection") && _mVal.equals("-1")) {
setContentView(R.layout.image);
//TextView text_bottom = (TextView) findViewById(R.id.textView2);
ImageView showImage = (ImageView) findViewById(R.id.imageView1);
//text_bottom.setText(Info[1]);
showImage.setImageResource(R.drawable.shift_down);
} else if (_mName.equals("recomGear") && _mVal != null) {
Integer msgValue = Integer.parseInt(_mVal);
Integer CurrentGear = (msgValue) - 1;
Log.d("TCP","in DA Images. Current gear: " + CurrentGear);
String Gear = Integer.toString(CurrentGear);
setContentView(R.layout.image);
TextView text_top = (TextView) findViewById(R.id.textView1);
TextView text_bottom = (TextView) findViewById(R.id.textView2);
ImageView showImage = (ImageView) findViewById(R.id.imageView1);
showImage.setImageResource(R.drawable.shift_up);
text_bottom.setText(Gear);
text_top.setText(_mVal);
//} //else if (_mName.equals("currGear") && _mVal != null) {
//PresentGear = _mVal;
//Log.d("TCP","in DA Images. Present gear1: " + PresentGear);
//setContentView(R.layout.image);
//TextView text_bottom = (TextView) findViewById(R.id.textView2);
//text_bottom.setText(_mVal);
} else if (_mName.equals("shiftDirection") && _mVal.equals("0")) {
Log.d("TCP","in DA Images. Present gear: " + PresentGear);
setContentView(R.layout.image);
TextView text_bottom = (TextView) findViewById(R.id.textView2);
TextView text_top = (TextView) findViewById(R.id.textView1);
text_top.setText("Go on");
text_bottom.setText(PresentGear);
}
}
}
更新:
谢谢大家的答复。我将发布从中获取字符串数组的部分。它可能不是最佳设计的代码。如果有任何建议,将很高兴。
public class TCPListen extends Activity implements TCPListener {
private TextView mTitle;
public String data[] = new String[2];
public String PGear;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
// Set up the window layout
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
// Set up the custom title
mTitle = (TextView) findViewById(R.id.title_left_text);
mTitle.setText(R.string.app_name);
mTitle = (TextView) findViewById(R.id.title_right_text);
//TcpServiceHandler handler=new TcpServiceHandler(this);
//handler.execute("192.168.62.23");
TcpServiceHandler handler = new TcpServiceHandler(this,this);
Thread th = new Thread(handler);
th.start();
}
public String[] callCompleted(String source){
Log.d("TCP", "Std parser " + source);
//mTitle.setText(source);
//String data[] = new String[2];
//if (source.matches("<MSG><N>.*</N><V>.*</V></MSG>")) {
Document doc = null;
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = (Document) db.parse(new ByteArrayInputStream(source.getBytes()));
NodeList n = doc.getElementsByTagName("N");
Node nd = n.item(0);
String msgName = nd.getFirstChild().getNodeValue();
NodeList n1 = doc.getElementsByTagName("V");
Node nd1 = n1.item(0);
String tmpVal = nd1.getFirstChild().getNodeValue();
data[0] = msgName;
data[1] = tmpVal;
Log.d("TCP", "Inside Std parser " + data[0] + " " + data[1]);
actionOnData(data[0], data[1]);
}
catch(Exception e){
e.printStackTrace();
}
Log.d("TCP", "Just outside Std parser " + data[0] + " " + data[1]);
return data;
//} else Log.d("TCP", "Message in wrong format " + source);
//mTitle.setText("Message in wrong format " + source);
//return data;
}
//Function to display driver messages/images based on individual messages
public void actionOnData(String name, String value) {
String tempName = name;
String tempVal = value;
if(tempName.equals("shiftDirection") && tempVal.equals("1")) {
Log.d("TCP","in actionOnData " + data[0] + " " + data[1]);
//mTitle.setText("Change to next higher gear");
Intent myIntent = new Intent();
myIntent.setClassName("com.example.android.TCPListen", "com.example.android.TCPListen.Images");
myIntent.putExtra("MsgInfo", data); // key/value pair, where key needs current package prefix.
startActivity(myIntent);
finish();
} else if(tempName.equals("recomGear")) {
Log.d("TCP","in actionOnData " + data[0] + " " + data[1]);
//mTitle.setText("Drive like a man");
Intent myIntent = new Intent();
myIntent.setClassName("com.example.android.TCPListen", "com.example.android.TCPListen.Images");
myIntent.putExtra("MsgInfo", data); // key/value pair, where key needs current package prefix.
startActivity(myIntent);
finish();
} else if(tempName.equals("shiftDirection") && tempVal.equals("-1")) {
Log.d("TCP","in actionOnData " + data[0] + " " + data[1]);
//mTitle.setText("Change to next higher gear");
Intent myIntent = new Intent();
myIntent.setClassName("com.example.android.TCPListen", "com.example.android.TCPListen.Images");
myIntent.putExtra("MsgInfo", data); // key/value pair, where key needs current package prefix.
startActivity(myIntent);
finish();
} else if(tempName.equals("currGear")) {
Log.d("TCP","in actionOnData " + data[0] + " " + data[1]);
//mTitle.setText("Change to next higher gear");
PGear = data[1];
Intent myIntent = new Intent();
myIntent.setClassName("com.example.android.TCPListen", "com.example.android.TCPListen.Images");
myIntent.putExtra("MsgInfo", data); // key/value pair, where key needs current package prefix.
startActivity(myIntent);
//finish();
} else if(tempName.equals("shiftDirection") && tempVal.equals("0")) {
Log.d("TCP","in actionOnData " + data[0] + " " + data[1]);
//mTitle.setText("Change to next higher gear");
data[1] = PGear;
Intent myIntent = new Intent();
myIntent.setClassName("com.example.android.TCPListen", "com.example.android.TCPListen.Images");
myIntent.putExtra("MsgInfo", data); // key/value pair, where key needs current package prefix.
startActivity(myIntent);
finish();
} else mTitle.setText("Just show something");
//Log.d("TCP", "Just show an image");
//}
}
}
实际上,在上面的代码中,我已经临时解决了currGear的值存储并在shiftDirection,0中使用它的问题。
最佳答案
PresentGear
的值将仅在Activity
的生存期内持续存在。如果操作系统杀死您的Activity
,则该值将丢失。您应该在onPause()
,onStop()
和onDestroy()
中进行一些登录,并检查应用程序的生命周期如何工作。您可能必须将PresentGear
的值写到SharedPreferences
中的onPause()
并在onCreate()
中读取结果。查看此链接以获取生命周期:
Application fundamentals