0

我目前有一个项目,其中主要活动启动另一个活动以收集信息,然后返回主要活动。我正在尝试处理后退按钮按下。每次我执行以下代码时,它都会崩溃。但我不明白为什么。

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

        finish();
        return true;
    }
    return super.onKeyDown(keyCode, event);
};

我也试过:

@Override
    public void onBackPressed() {
            finish();
    };

堆栈跟踪:

09-14 15:30:13.740: E/AndroidRuntime(32400): FATAL EXCEPTION: main
09-14 15:30:13.740: E/AndroidRuntime(32400): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {peaBerry.Shopping.Consultant/peaBerry.Shopping.Consultant.PeaBerryTeaActivity}: java.lang.NullPointerException
09-14 15:30:13.740: E/AndroidRuntime(32400):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2655)
09-14 15:30:13.740: E/AndroidRuntime(32400):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2697)
09-14 15:30:13.740: E/AndroidRuntime(32400):    at android.app.ActivityThread.access$2000(ActivityThread.java:124)
09-14 15:30:13.740: E/AndroidRuntime(32400):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:998)
09-14 15:30:13.740: E/AndroidRuntime(32400):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-14 15:30:13.740: E/AndroidRuntime(32400):    at android.os.Looper.loop(Looper.java:130)
09-14 15:30:13.740: E/AndroidRuntime(32400):    at android.app.ActivityThread.main(ActivityThread.java:3806)
09-14 15:30:13.740: E/AndroidRuntime(32400):    at java.lang.reflect.Method.invokeNative(Native Method)
09-14 15:30:13.740: E/AndroidRuntime(32400):    at java.lang.reflect.Method.invoke(Method.java:507)
09-14 15:30:13.740: E/AndroidRuntime(32400):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-14 15:30:13.740: E/AndroidRuntime(32400):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-14 15:30:13.740: E/AndroidRuntime(32400):    at dalvik.system.NativeStart.main(Native Method)
09-14 15:30:13.740: E/AndroidRuntime(32400): Caused by: java.lang.NullPointerException
09-14 15:30:13.740: E/AndroidRuntime(32400):    at peaBerry.Shopping.Consultant.PeaBerryTeaActivity.onActivityResult(PeaBerryTeaActivity.java:312)
09-14 15:30:13.740: E/AndroidRuntime(32400):    at android.app.Activity.dispatchActivityResult(Activity.java:3958)
09-14 15:30:13.740: E/AndroidRuntime(32400):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2651)
4

1 回答 1

3

根据错误消息,缺少结果。你开始第二个活动了startActivityForResult()吗?如果是这样,请在调用第二个 Activity 之前添加setResult(value)setResult(value, data)finish(),以将预期结果传递回调用 Activity。

于 2012-09-14T19:35:46.383 回答