Question

Im having some problems when sending an arrayList of objects to another activity on press of the back button. The weird thing is that i can send this arrayList from the first activity to the second and use it. But when I try to send it back (back button), the app crashes. This is how I do it:

Sending the arrayList (and a string) from activity 1:

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

And receiving this in the second activity:

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

In this activity I'm doing the following to send it back:

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

And to receive the above (back in activity 1) I'm doing this:

 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 ...
                }
       }
}

It is when I tab the back button the app crashes and I really can't see what's wrong. So I really hope anyone figure this out.

Here is my log output (after the crash) if you need it:

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
Was it helpful?

Solution

use it. removed the line getIntent and used data which you receive in the onActivityResult callback.

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 ...
            }
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top