vikramkakkar / SublimeNavigationView

...is a complete rewrite of NavigationView (from Design Support library) that enables - usage of Checkboxes, Switches & Badges as menu items - XML definition - Parcelable menus - collapsible/expandable menu-groups - multiple menus - custom styling.
Apache License 2.0
309 stars 42 forks source link

NullPointerException: at readParcel #8

Closed alashow closed 8 years ago

alashow commented 8 years ago

Crashes sometimes with following exception:

java.lang.RuntimeException: Unable to start activity ComponentInfo{tm.asmanoky.aziada/tm.asmanoky.aziada.ui.activity.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                        at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                     Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
                                                                        at android.os.Parcel.readTypedList(Parcel.java:2029)
                                                                        at com.appeaser.sublimenavigationviewlibrary.SublimeMenu.readParcel(SublimeMenu.java:1321)
                                                                        at com.appeaser.sublimenavigationviewlibrary.SublimeMenu.<init>(SublimeMenu.java:1315)
                                                                        at com.appeaser.sublimenavigationviewlibrary.SublimeMenu$2.createFromParcel(SublimeMenu.java:1348)
                                                                        at com.appeaser.sublimenavigationviewlibrary.SublimeMenu$2.createFromParcel(SublimeMenu.java:1346)
                                                                        at android.os.Parcel.readParcelable(Parcel.java:2367)
                                                                        at android.os.Parcel.readValue(Parcel.java:2264)
                                                                        at android.os.Parcel.readArrayMapInternal(Parcel.java:2614)
                                                                        at android.os.BaseBundle.unparcel(BaseBundle.java:221)
                                                                        at android.os.BaseBundle.getBoolean(BaseBundle.java:659)
                                                                        at android.app.Activity.restoreHasCurrentPermissionRequest(Activity.java:6444)
                                                                        at android.app.Activity.performCreate(Activity.java:6250)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:148) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
vikramkakkar commented 8 years ago

Although I am not able to produce this exception on my end [API 22, Nexus 4], I may have located the problem.

SublimeMenu implements Parcelable and provides the CREATOR:

public static final Creator CREATOR = new Creator() {
    public SublimeMenu createFromParcel(Parcel in) {
        return new SublimeMenu(in);
    }

    public SublimeMenu[] newArray(int size) {
        return new SublimeMenu[size];
    }
};

The constructor Sublime(Parcel):

public SublimeMenu(Parcel in) {
    readParcel(in);
}

private void readParcel(Parcel in) {
    mMenuResourceID = in.readInt();

    // Exception if `mItems` is null here
    in.readTypedList(mItems, SublimeBaseMenuItem.CREATOR);

    for (SublimeBaseMenuItem item : mItems) {
        item.setParentMenu(this);
    }

    // Exception if `mGroups` is null here
    in.readTypedList(mGroups, SublimeGroup.CREATOR);

    for (SublimeGroup group : mGroups) {
        group.setParentMenu(this);
    }
}

This is good if mItems & mGroups are initialized. The exception gets thrown if they are null.

As a fix, I am initializing mitems & mGroups outside the constructor now. I will push this along the next update.

I am closing this for now. We'll revisit this if the problem persists after the update.

alashow commented 8 years ago

could you please push it as snapshot or something like that. thanks.

vikramkakkar commented 8 years ago

I have pushed this on the dev branch. You can access it using Jitpack:

In your project's build.gradle:

allprojects {
    repositories {
        jcenter()
        ....
        ....
        maven { url "https://jitpack.io" }
    }
}

In your app's build.gradle:

dependencies {
    ....
    ....
    compile 'com.github.vikramkakkar:SublimeNavigationView:dev-SNAPSHOT'
}

Thanks for pointing this out!