我正在使用onActivityResult从先前的意图中获取数据并且它工作正常。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case (11): {
newPosition = data.getIntExtra("position", 0);
break;
}
}
}
但是,当我使用操作栏后退按钮返回到先前的意图时,会出现问题。它出现以下错误:
01-10 15:33:34.128: E/AndroidRuntime(642): FATAL EXCEPTION: main
01-10 15:33:34.128: E/AndroidRuntime(642): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=11, result=0, data=null} to activity {com.cogneti/com.cogneti.ui.recall.TestActivity}: java.lang.NullPointerException
01-10 15:33:34.128: E/AndroidRuntime(642): at android.app.ActivityThread.deliverResults(ActivityThread.java:2726)
01-10 15:33:34.128: E/AndroidRuntime(642): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2769)
01-10 15:33:34.128: E/AndroidRuntime(642): at android.app.ActivityThread.access$2000(ActivityThread.java:123)
01-10 15:33:34.128: E/AndroidRuntime(642): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1023)
01-10 15:33:34.128: E/AndroidRuntime(642): at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 15:33:34.128: E/AndroidRuntime(642): at android.os.Looper.loop(Looper.java:126)
01-10 15:33:34.128: E/AndroidRuntime(642): at android.app.ActivityThread.main(ActivityThread.java:3997)
01-10 15:33:34.128: E/AndroidRuntime(642): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 15:33:34.128: E/AndroidRuntime(642): at java.lang.reflect.Method.invoke(Method.java:491)
01-10 15:33:34.128: E/AndroidRuntime(642): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-10 15:33:34.128: E/AndroidRuntime(642): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-10 15:33:34.128: E/AndroidRuntime(642): at dalvik.system.NativeStart.main(Native Method)
01-10 15:33:34.128: E/AndroidRuntime(642): Caused by: java.lang.NullPointerException
01-10 15:33:34.128: E/AndroidRuntime(642): at com.cogneti.ui.recall.TestActivity.onActivityResult(TestActivity.java:221)
01-10 15:33:34.128: E/AndroidRuntime(642): at android.app.Activity.dispatchActivityResult(Activity.java:4483)
01-10 15:33:34.128: E/AndroidRuntime(642): at android.app.ActivityThread.deliverResults(ActivityThread.java:2722)
01-10 15:33:34.128: E/AndroidRuntime(642): ... 11 more
使用默认后退按钮时工作正常。
我在onBackPressed()方法中设置了结果。
@Override
public void onBackPressed() {
Intent resultIntent = new Intent();
resultIntent.putExtra("position", selectedPosition);
setResult(Activity.RESULT_OK, resultIntent);
super.onBackPressed();
};
答案 0 :(得分:2)
好的,
使用代码创建结果数据的函数。
Intent resultIntent = new Intent();
resultIntent.putExtra("position", selectedPosition);
setResult(Activity.RESULT_OK, resultIntent);
并从之前活动的onStop()
或onDestroy()
调用此函数。
实际上我认为当您从操作栏按下后退时public void onBackPressed()
未被调用,因此您必须在活动的onStop()
或onDestroy()
中调用此代码。
<强>更新强>
我认为当你按回动作栏
时会调用@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
// Here write your set Result on Intent code..
finish();
return true;
}
return super.onOptionsItemSelected(item);
}