0

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

1 回答 1

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);
}
于 2013-01-10T10:28:27.260 回答