2

I'm getting this error when I'm trying to getExtra from intent. getIntent().getParcelableArrayListExtra(KEY)

When I change the KEY then the error comes to be java.lang.NullPointerException. I'm not able to understand why this is happening. There are many Parcelable objects which are working fine. This was also working fine (So, there is no problem with the Parcelable object) until I put other objects in intent with different key-value pairs and also might have done something wrong. Can someone tell me why this error happening? And what can be the possible reason behind this type of error?

you can check the logs also.

02-24 02:55:21.927 28362-28362/com.abc.app E/AndroidRuntime: FATAL EXCEPTION: main
     Process: com.abc.app, PID: 28362
     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.app/com.abc.app.activity.FinalActivity}: java.lang.IllegalArgumentException: Duplicate key in ArrayMap: null
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2493)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2555)
         at android.app.ActivityThread.access$800(ActivityThread.java:176)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1437)
         at android.os.Handler.dispatchMessage(Handler.java:111)
         at android.os.Looper.loop(Looper.java:194)
         at android.app.ActivityThread.main(ActivityThread.java:5576)
         at java.lang.reflect.Method.invoke(Native Method)
         at java.lang.reflect.Method.invoke(Method.java:372)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
      Caused by: java.lang.IllegalArgumentException: Duplicate key in ArrayMap: null
         at android.util.ArrayMap.validate(ArrayMap.java:555)
         at android.os.Parcel.readArrayMapInternal(Parcel.java:2486)
         at android.os.BaseBundle.unparcel(BaseBundle.java:221)
         at android.os.Bundle.getParcelableArrayList(Bundle.java:848)
         at android.content.Intent.getParcelableArrayListExtra(Intent.java:5099)
         at com.abc.app.activity.FinalActivity.onCreate(FinalActivity.java:60)
         at android.app.Activity.performCreate(Activity.java:6005)
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2446)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2555) 
         at android.app.ActivityThread.access$800(ActivityThread.java:176) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1437) 
         at android.os.Handler.dispatchMessage(Handler.java:111) 
         at android.os.Looper.loop(Looper.java:194) 
         at android.app.ActivityThread.main(ActivityThread.java:5576) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at java.lang.reflect.Method.invoke(Method.java:372) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751) 

And when I change the key I get this in log along with null pointer exception

02-24 03:01:38.074 32206-32206/com.abc.app W/ArrayMap: New hash 0 is before end of array hash 1374673561 at index 4 key null
     java.lang.RuntimeException: here
         at android.util.ArrayMap.append(ArrayMap.java:510)
         at android.os.Parcel.readArrayMapInternal(Parcel.java:2483)
         at android.os.BaseBundle.unparcel(BaseBundle.java:221)
         at android.os.Bundle.getParcelableArrayList(Bundle.java:848)
         at android.content.Intent.getParcelableArrayListExtra(Intent.java:5099)
         at com.abc.app.activity.FinalActivity.onCreate(FinalActivity.java:60)
         at android.app.Activity.performCreate(Activity.java:6005)
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2446)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2555)
         at android.app.ActivityThread.access$800(ActivityThread.java:176)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1437)
         at android.os.Handler.dispatchMessage(Handler.java:111)
         at android.os.Looper.loop(Looper.java:194)
         at android.app.ActivityThread.main(ActivityThread.java:5576)
         at java.lang.reflect.Method.invoke(Native Method)
         at java.lang.reflect.Method.invoke(Method.java:372)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
Trinimon
  • 13,839
  • 9
  • 44
  • 60
anubh
  • 649
  • 7
  • 12

6 Answers6

1

In my case, I was writing the same key twice inside writeToParcel method of my class Model which implemented Parcelable. So it threw me error while it was being saved/retrieved from Bundle.

Wrong code:

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(feature_id);
    dest.writeString(tab_custom_name);
    dest.writeString(sort_order);
    dest.writeString(tab_logo_selected_image);
    dest.writeString(tab_logo_unselected_image);
    dest.writeString(tab_logo_unselected_image);

So, i removed one of the two similar lines written in the End. Works Error free afterwards.

sud007
  • 5,824
  • 4
  • 56
  • 63
1

Write to Parcelable and read from Parcelable should be in same order. Or you put null in extras.

Marriage
  • 501
  • 3
  • 15
0

There was something wrong with my Parcelable class. I remade it and now it's working fine.

anubh
  • 649
  • 7
  • 12
0

In my case,because server language is php and it's return json String is not standard.For example,field 'code' may String or int,So I define 'code' as this Object code.But when class implements Parcelable,you must specify the field type,if not,you will receive thie error.

In addition,my solution is define field 'localCode' to use and parcel it.I set 'localCode' field when I get server return data.

Of Course,the best solution is ensure server return standard json.

turbofan
  • 318
  • 3
  • 7
0

In my case, I had two different constructor for class which implements Parcelable, and generated @Override writeToParcel and read parcel method using IDE, they had different number of parameters. so I fixed the issues by having same parameters (same number, type and order) deleted the extra parameters, see the commented part in the code.

    public class MediaChannel implements Parcelable {
    String name;
    String url;
    String supportDevice;
    String process;
    String line;
    String icon;

    // movies extra fields
    String imagePath;
    String category;
    String duration;
    String title;
    String description;
    String actors;
    String years;
    String imdb;

    public MediaChannel(String name, String url, String process, String icon, String imagePath, String category, String duration, String title, String description, String actors, String years, String imdb) {
        this.name = name;
        this.url = url;
        this.process = process;
        this.icon = icon;
        this.imagePath = imagePath;
        this.category = category;
        this.duration = duration;
        this.title = title;
        this.description = description;
        this.actors = actors;
        this.years = years;
        this.imdb = imdb;
    }

    public MediaChannel(String name, String url, String supportDevice, String process, String line) {
        this.name = name;
        this.url = url;
        this.supportDevice = supportDevice;
        this.process = process;
        this.line = line;
    }

    protected MediaChannel(Parcel in) {
        name = in.readString();
        url = in.readString();
        supportDevice = in.readString();
        process = in.readString();
        line = in.readString();
    }


    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(url);
        dest.writeString(supportDevice);
        dest.writeString(process);
        dest.writeString(line);
// deleted this part to fix the issues 
/*
       dest.writeString(icon);
       dest.writeString(imagePath);
        dest.writeString(category);
       dest.writeString(duration);
        dest.writeString(title);
       dest.writeString(description);
       dest.writeString(actors);
       dest.writeString(years);
        dest.writeString(imdb);
*/
    }
}
solidfox
  • 581
  • 1
  • 6
  • 20
0

I fixed it by removing proguardFiles line from build.gradle. Reference: https://stackoverflow.com/a/22761017/2054348 https://stackoverflow.com/a/21380449/2054348

Sagar Shah
  • 4,272
  • 2
  • 25
  • 36