Friday, April 18, 2014

An activity to receive data from the two activity, always reported null pointer fault, seek help!


My intention first described an activity page, two buttons b1, b2, b1 click into activity1, get data name1, return to activity, showing the activity pages at this picture, then click the b2 into activity2, get checked Data name2, also returns to the activity of the page, because I want to write an algorithm based on the comparison of the two returned to the data, but now an intent to take two packets null pointer errors, I studied for a long time, do not know how to solve, Or is there any other ideas, to gratitude!
Here is the main activity in the intent to achieve segments:
 intent = this.getIntent (); 
bundle = intent.getExtras ();
name1 = bundle.getString ("name1");
name2 = bundle.getString ("name2");
System.out.println (name1);
if (name1.equals ("zhaoyun")) {
insertPicture1.insertDrawable (R.drawable.zhaoyun);
}

if (name2.equals ("machao")) {
insertPicture2.insertDrawable (R.drawable.machao);
}


Thank you very much! ! !
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
When testing a single activity, the data can be returned to normal, but do not know how to deal with two activity data, is not so write ah?
Reply:
Bundle b = getIntent () getExtras ();.
if (b! = null && b.containsKey (key)) {
String str = b.getString (key);
}
Reply:
cited landlord minglaihan reply:
first explain my intention, an activity page, two buttons b1, b2, b1 click into activity1, get the data name1, return to activity, showing the activity pages at this picture, then click the b2 into activity2, select get data name2, return to the activity of the same page, because I want to return based on these two data write a comparison algorithm, But now an intent to take two packets null pointer errors, I studied for a long time, do not know how to solve, or have any other ideas, to gratitude!
Here is the main activity in the intent to achieve segments:
 intent = this.getIntent (); 
bundle = intent.getExtras ();
name1 = bundle.getString ("name1");
name2 = bundle.getString ("name2");
System.out.println (name1);
if (name1.equals ("zhaoyun")) {
insertPicture1.insertDrawable (R.drawable.zhaoyun);
}

if (name2.equals ("machao")) {
insertPicture2.insertDrawable (R.drawable.machao);
}


Thank you very much! ! !
From your description, an activity can only return a value, you have to get this look two values, both values ​​are obtained from an activity?
Reply:
Short judgment, otherwise there is always an empty two
Reply:
reference to the second floor mmorss reply:
Bundle b = getIntent () getExtras ();
.if (b! = null && b.containsKey (key)) {
String str = b.getString (key);
}


 intent = this.getIntent (); 
bundle = intent.getExtras ();
System.out.println ("Test" + bundle.containsKey (name1)) ;/ / returns false
System.out.println (bundle.getString ("name1")) ;/ / print zhaoyun, the outcome
if (bundle! = null && bundle.containsKey (name1)) {

name1 = bundle.getString ("name1");

if (name1.equals ("zhaoyun")) {
insertPicture1.insertDrawable (R.drawable.zhaoyun);
System.out.println ("Yse");
}
}


But why bundle.containsKey (name1) returns false?
Reply:
reference to the third floor danielinbiti reply:
Quote: Reply with quote landlord minglaihan of:

My intention first described an activity page, two buttons b1, b2, b1 click into activity1, get data name1, return to activity, showing the activity pages at this picture, then click the b2 into activity2, get checked Data name2, also returns to the activity of the page, because I want to write an algorithm based on the comparison of the two returned to the data, but now an intent to take two packets null pointer errors, I studied for a long time, do not know how to solve, Or is there any other ideas, to gratitude!
Here is the main activity in the intent to achieve segments:
 intent = this.getIntent (); 
bundle = intent.getExtras ();
name1 = bundle.getString ("name1");
name2 = bundle.getString ("name2");
System.out.println (name1);
if (name1.equals ("zhaoyun")) {
insertPicture1.insertDrawable (R.drawable.zhaoyun);
}

if (name2.equals ("machao")) {
insertPicture2.insertDrawable (R.drawable.machao);
}


Thank you very much! ! !
From your description, an activity can only return a value, you have to get this look two values, both values ​​are obtained from an activity?


Are two different activity each pass a value to the same activity
Reply:
Does not include, of course, a false return
Reply:
From the time the second Activity returned, intent has not returned from the first activity of the intent. Because when you returned from the second Activity is not passed name1, fail to name1, so here will be reported null pointer exception.
You can do this from the first Activity to return later to save up name1. , The name2 name1 taken out and saved from when comparing the second Activity returned.
Reply:
Static variable assignment in pass activity
Reply:
Were sentenced to empty talk about it
Reply:
Amended as follows: to see if a suitable

 intent = this.getIntent (); 
bundle = intent.getExtras ();
name1 = bundle.getString ("name1");
name2 = bundle.getString ("name2");
System.out.println (name1);
if ("zhaoyun". equals (name1)) {
insertPicture1.insertDrawable (R.drawable.zhaoyun);
} Else if ("machao". Equals (name2)) {
insertPicture2.insertDrawable (R.drawable.machao);
}

Reply:
It can be used intent of startActivityForResult ();
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
String result = data.getExtras (). GetString ("result")) ;/ / get the data
returned after closing the new Activity}, You can see the specific use or under development documents found next example. Than you are now using that method is much simpler

Reply:
Ultimately depend on the first save and then taken out name1 name2, ahead of empty judgment, thank you

No comments:

Post a Comment