Android: Failure delivering result ResultInfo

匿名 (未验证) 提交于 2019-12-03 01:26:01

问题:

I have an Activity A in TabHost, A startActivityForResult(B) then B startActivityForResult(C).
The problem I meet is that:
In C when I setResult(RESULT_OK, intent), occur

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { (has extras) }} to activity {com.hackingtrace.vancexu/com.hackingtrace.vancexu.task.B}: java.lang.NullPointerException 

In B, I start C by

setTimeButton.setOnClickListener(new View.OnClickListener() {          public void onClick(View view) {             Intent i = new Intent(view.getContext(), TaskTimeSet.class);             i.putExtra(TasksDbAdapter.KEY_ROWID, mRowId);             Calendar c = Calendar.getInstance();             Date d = c.getTime();                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");             String now = sdf.format(d);             i.putExtra("date", now);              startActivityForResult(i, ACTIVITY_EDIT_TIME);         } 

and in C, I setResult by

confirmBtn.setOnClickListener(new OnClickListener() {          public void onClick(View v) {             // TODO Auto-generated method stub             Intent intent = new Intent();             intent.putExtra("date", resultDate);              int h = timePicker.getCurrentHour();             int m = timePicker.getCurrentMinute();             String time = "" +h+":"+m;             Log.d(TAG, time);             intent.putExtra("time", time);              setResult(RESULT_OK, intent);             finish();         }     }); 

then in B I receive the result by

protected void onActivityResult(int requestCode, int resultCode,         Intent intent) {     Log.d(TAG, "I am in " + requestCode + resultCode);     super.onActivityResult(requestCode, resultCode, intent);     if (requestCode == ACTIVITY_EDIT_TIME) {         if (resultCode == RESULT_OK) {             Log.d(TAG, "OK time set");             String[] dateArr = intent.getStringExtra("date").split("-");             String[] timeArr = intent.getStringExtra("time").split(":");             int year = Integer.parseInt(dateArr[0]);             int month = Integer.parseInt(dateArr[1]);             int day = Integer.parseInt(dateArr[2]);             int hour = Integer.parseInt(timeArr[0]);             int minute = Integer.parseInt(timeArr[1]);             Log.d(TAG, ""+year+month+day+hour+minute);                       }     } } 

But I never get the "Log.d(TAG, ""+year+month+day+hour+minute);" run normally.

I have tried:
1:

if (getParent() == null) {                 setResult(Activity.RESULT_OK, intent);             }             else {                 getParent().setResult(Activity.RESULT_OK, intent);             } 

not work :(
2:

i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

the error "java.lang.RuntimeException" not occur, but the requestCode and resultCode is 0 always.

回答1:

In C I putExtra 2 string (resultDate and time), but I miss the point that resultDate may not be initialized when it is be put into intent. So I got the Error: java.lang.NullPointerException.



回答2:

It looks like your Activity B is getting destroyed before C returns. One thing you can check is the "Dev Tools" app (It's among the app icons on most emulators and devices and in Settings on Ice Cream Sandwich labeled as "Developer Options") to make sure that the "Don't keep activities" option is not checked.

One thing you can do to verify that B is getting destroyed is to put a Log statement in Activity B's onCreate method. Then, when Activity C is launched from B, exit C by pushing the hardware Back button. This will destroy C (provided you haven't overridden the Back button), but it will not call B's onCreate unless B has been destroyed.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!