我正在使用 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();
};