Reputation: 162
Please go through the code below.
String[] Info
contains 2 values like this:
shiftDirection & 1
or currGear & 5
and similar pairs. If I receive shiftDirection & 0
, I should display the previously received value of currGear
.
So I create PresentGear
as a global variable and when currGear
is received, I store it's value in PresentGear
.
Then when I receive shiftDirection & 0
, I try to display PresentGear
but nothing gets displayed.
PresentGear
is assigned no value. It would be very helpful if someone could suggest why this is happening or if my approach is wrong, kindly suggest an alternative way.
Cheers,
Madhu
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);
}
}
}
Update:
Thank you all for the response. I will post the part where I am getting the string array from. It may not be the best designed code. Would be happy if any changes are suggested.
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");
//}
}
}
In fact, in the above code, I have temporarily solved the issue of storing value of currGear and using it in shiftDirection, 0.
Upvotes: 0
Views: 1000
Reputation: 10908
The value of PresentGear
will only persist for the lifetime of your Activity
. If the OS kills your Activity
then the value will be lost. You should put some logging in onPause()
, onStop()
and onDestroy()
and check how the lifecycle of your app is working. You may have to write out the value of PresentGear
to SharedPreferences
in onPause()
and read the result in your onCreate()
. Check out this link for the lifecycle:
Upvotes: 1
Reputation: 234797
Sounds like you should step through this code in the debugger and examine what values are being delivered to this activity. How are you constructing the Intent that gets delivered? (I suspect that either Info[0]
isn't "currGear"
or Info[1]
is ""
, but something else could be going on.)
Upvotes: 1