0

在按下后退按钮将对象数组列表发送到另一个活动时,我遇到了一些问题。奇怪的是,我可以将这个 arrayList 从第一个活动发送到第二个活动并使用它。但是当我尝试将其发回(后退按钮)时,应用程序崩溃了。我就是这样做的:

从活动 1 发送 arrayList(和一个字符串):

Intent intent = new Intent(v.getContext(), ShowDay.class);
        intent.putExtra("exerciseList", list);
        intent.putExtra("day", selectedFromList);
        startActivityForResult(intent, EDIT_TRAININGDAY_RESPONSE);

并在第二个活动中收到:

Intent intent = getIntent();
        exercises = (ArrayList<Exercise>) intent.getSerializableExtra("exerciseList");
        day = intent.getStringExtra("day");

在此活动中,我正在执行以下操作以将其发回:

 @Override
      public void onBackPressed(){        
            Intent intent = new Intent();
            intent.putExtra("exerciseList",exercises);
            intent.putExtra("day", day);
            setResult(RESULT_OK, intent);
            finish();
       }

并收到上述内容(回到活动 1)我正在这样做:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         if (requestCode == EDIT_TRAININGDAY_RESPONSE) {
             if (resultCode == RESULT_OK) {

                Intent intent = getIntent();
                ArrayList<Exercise> newExerciseList = (ArrayList<Exercise>) intent.getSerializableExtra("exerciseList");
                String currentDay = intent.getStringExtra("day");
                     // Other code ...
                }
       }
}

当我点击后退按钮时,应用程序崩溃了,我真的看不出有什么问题。所以我真的希望有人能弄清楚这一点。

如果需要,这是我的日志输出(崩溃后):

12-26 18:10:56.151: E/AndroidRuntime(18849): FATAL EXCEPTION: main
12-26 18:10:56.151: E/AndroidRuntime(18849): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { (has extras) }} to activity {com.pack.androidexam/com.pack.androidexam.ShowPickedProgram}: java.lang.NullPointerException
12-26 18:10:56.151: E/AndroidRuntime(18849):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3141)
12-26 18:10:56.151: E/AndroidRuntime(18849):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
12-26 18:10:56.151: E/AndroidRuntime(18849):    at android.app.ActivityThread.access$1100(ActivityThread.java:130)
12-26 18:10:56.151: E/AndroidRuntime(18849):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
12-26 18:10:56.151: E/AndroidRuntime(18849):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 18:10:56.151: E/AndroidRuntime(18849):    at android.os.Looper.loop(Looper.java:137)
12-26 18:10:56.151: E/AndroidRuntime(18849):    at android.app.ActivityThread.main(ActivityThread.java:4745)
12-26 18:10:56.151: E/AndroidRuntime(18849):    at java.lang.reflect.Method.invokeNative(Native Method)
12-26 18:10:56.151: E/AndroidRuntime(18849):    at java.lang.reflect.Method.invoke(Method.java:511)
12-26 18:10:56.151: E/AndroidRuntime(18849):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-26 18:10:56.151: E/AndroidRuntime(18849):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-26 18:10:56.151: E/AndroidRuntime(18849):    at dalvik.system.NativeStart.main(Native Method)
12-26 18:10:56.151: E/AndroidRuntime(18849): Caused by: java.lang.NullPointerException
12-26 18:10:56.151: E/AndroidRuntime(18849):    at com.pack.androidexam.ShowPickedProgram.onActivityResult(ShowPickedProgram.java:98)
12-26 18:10:56.151: E/AndroidRuntime(18849):    at android.app.Activity.dispatchActivityResult(Activity.java:5192)
12-26 18:10:56.151: E/AndroidRuntime(18849):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
12-26 18:10:56.151: E/AndroidRuntime(18849):    ... 11 more
4

1 回答 1

0

用它。删除了 getIntent 行并使用了您在 onActivityResult 回调中收到的数据。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     if (requestCode == EDIT_TRAININGDAY_RESPONSE) {
         if (resultCode == RESULT_OK) {

            //Intent intent = getIntent();
            ArrayList<Exercise> newExerciseList = (ArrayList<Exercise>) data.getSerializableExtra("exerciseList");
            String currentDay = intent.getStringExtra("day");
                 // Other code ...
            }
   }
}
于 2013-12-26T19:02:56.610 回答