2013年9月18日星期三

[Android] An activity you want to receive data from the two activity , always reported null pointer fault, seek help !

First described my intention, an activity page, two buttons b1, b2, b1 Click to enter activity1, get the data name1, return to activity, activity at this time out of the picture displayed on the page , then click the b2 into activity2, get checked data name2, also returns to the activity of the page , because I want to return to based on these two data write a comparison algorithm, but now an intent to take two packets null pointer error , I studied a long time , do not know how to solve , or is there any other ideas to to thank !
Here is the main activity in the intent to achieve paragraph :
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 ! ! !
------ Solution ---------------------------------------- ----
Bundle b = getIntent (). getExtras ();
if (b! = null && b.containsKey (key)) {
String str = b.getString (key);
}
------ Solution ----------------------------------- ---------
short judgment , or two there is always an empty
------ Solution ----------------- ---------------------------
Activity returned from the second time , intent is not to return from the first activity of intent the . Because you return from the second Activity when there is no transfer name1, fail to name1, so here will be reported null pointer exception.
You can do this from the first Activity Back to the future , the name1 saved. Activity returned from the second , when taken out of the name2 name1 compare and save .
------ Solution ---------------------------------------- ----
were sentenced empty talk about it
------ Solution ---------------------------- ----------------
can do with the intent of startActivityForResult ();
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
String result = data.getExtras (). getString ("result")) ;/ / get the new Activity after the closure of the returned data
}, specific methods you can go to the next document, or search under development examples . Than you are now using that method is much simpler

------ eference ------------------------------------ ---
single activity in the test , the data can return to normal , but do not know how to deal with two activity data , is not no way to write ah ?
------ eference --------------------------------------- < br> from your description, an activity can only return a value , you get this look both values ​​, both values ​​are obtained from an activity ?
------ eference --------------------------------------- < br>

intent = this.getIntent();		
bundle = intent.getExtras();
System.out.println("Test"+bundle.containsKey(name1));//返回false
System.out.println(bundle.getString("name1"));//打印zhaoyun,有结果
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) will return false?
------ eference --------------------------------------- < br> from your description, an activity can only return a value , you get this look both values ​​, both values ​​are obtained from an activity ?  

are two different activity each pass a value to the same activity
------ eference ---------------------- -----------------
does not include , of course, returns false the
------ eference ------------- --------------------------
static variable assignment in pass activity
------ eference - -------------------------------------
modified as follows: to see whether appropriate

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);
        }

------ eference ------------------------------------- -
ultimately depend on the first save and then taken out name1 name2, ahead of empty judgment , thank you

没有评论:

发表评论