tjerkw / Android-SlideExpandableListView

A better ExpandableListView, with animated expandable views for each list item
Apache License 2.0
1.98k stars 741 forks source link

Null Pointer at line 298 in AbstractSlideExpandableListAdapter.writeBitSet #32

Closed marcorighini closed 10 years ago

marcorighini commented 11 years ago

Hi, sometimes I got a null pointer exception at line 298 in AbstractSlideExpandableListAdapter.writeBitSet, immediately after rotating or waking up from sleep mode.

Here is the stacktrace:

java.lang.RuntimeException: Unable to start activity ComponentInfo{....ContainerActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter.writeBitSet(AbstractSlideExpandableListAdapter.java:298)
at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter.access$0(AbstractSlideExpandableListAdapter.java:295)
at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter$SavedState.<init>(AbstractSlideExpandableListAdapter.java:319)
at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter$SavedState.<init>(AbstractSlideExpandableListAdapter.java:316)
at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter$SavedState$1.createFromParcel(AbstractSlideExpandableListAdapter.java:333)
at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter$SavedState$1.createFromParcel(AbstractSlideExpandableListAdapter.java:1)
at android.os.Parcel.readParcelable(Parcel.java:2103)
at android.os.Parcel.readValue(Parcel.java:1965)
at android.os.Parcel.readSparseArrayInternal(Parcel.java:2255)
at android.os.Parcel.readSparseArray(Parcel.java:1687)
at android.os.Parcel.readValue(Parcel.java:2022)
at android.os.Parcel.readMapInternal(Parcel.java:2226)
at android.os.Bundle.unparcel(Bundle.java:223)
at android.os.Bundle.getSparseParcelableArray(Bundle.java:1240)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:845)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1856)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:210)
at com.github.rtyley.android.sherlock.roboguice.activity.RoboSherlockFragmentActivity.onCreate(RoboSherlockFragmentActivity.java:34)
at ......ContainerActivity.onCreate(ContainerActivity.java:90)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more
java.lang.NullPointerException
at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter.writeBitSet(AbstractSlideExpandableListAdapter.java:298)
at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter.access$0(AbstractSlideExpandableListAdapter.java:295)
at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter$SavedState.<init>(AbstractSlideExpandableListAdapter.java:319)
at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter$SavedState.<init>(AbstractSlideExpandableListAdapter.java:316)
at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter$SavedState$1.createFromParcel(AbstractSlideExpandableListAdapter.java:333)
at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter$SavedState$1.createFromParcel(AbstractSlideExpandableListAdapter.java:1)
at android.os.Parcel.readParcelable(Parcel.java:2103)
at android.os.Parcel.readValue(Parcel.java:1965)
at android.os.Parcel.readSparseArrayInternal(Parcel.java:2255)
at android.os.Parcel.readSparseArray(Parcel.java:1687)
at android.os.Parcel.readValue(Parcel.java:2022)
at android.os.Parcel.readMapInternal(Parcel.java:2226)
at android.os.Bundle.unparcel(Bundle.java:223)
at android.os.Bundle.getSparseParcelableArray(Bundle.java:1240)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:845)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1856)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:210)
at com.github.rtyley.android.sherlock.roboguice.activity.RoboSherlockFragmentActivity.onCreate(RoboSherlockFragmentActivity.java:34)
at .....ContainerActivity.onCreate(ContainerActivity.java:90)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
CandidoCaballero commented 11 years ago

Hello, I have an app with SlideExpandableListView (without fragments) and I also have a similar error.

java.lang.RuntimeException: Unable to start activity ComponentInfo{...XXActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2836) at android.app.ActivityThread.access$1600(ActivityThread.java:117) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:130) at android.app.ActivityThread.main(ActivityThread.java:3687) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.tjerkw.slideexpandable.library.SlideExpandableListView.onRestoreInstanceState(SlideExpandableListView.java:81) at com.tjerkw.slideexpandable.library.ActionSlideExpandableListView.onRestoreInstanceState(ActionSlideExpandableListView.java:1) at android.view.View.dispatchRestoreInstanceState(View.java:6278) at android.view.ViewGroup.dispatchThawSelfOnly(ViewGroup.java:1228) at android.widget.AdapterView.dispatchRestoreInstanceState(AdapterView.java:767) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1215) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1215) at android.view.View.restoreHierarchyState(View.java:6257) at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1564) at android.app.Activity.onRestoreInstanceState(Activity.java:844) at android.app.Activity.performRestoreInstanceState(Activity.java:816) at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1096) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1629) ... 12 more

MattWilliams89 commented 11 years ago

Ditto:

java.lang.RuntimeException: Unable to start activity ComponentInfo{org.MAW.yourclubfree/org.MAW.yourclubfree.Activity_Base}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106) at android.app.ActivityThread.access$700(ActivityThread.java:134) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1217) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4856) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter.writeBitSet(AbstractSlideExpandableListAdapter.java:269) at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter.access$400(AbstractSlideExpandableListAdapter.java:22) at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter$SavedState.(AbstractSlideExpandableListAdapter.java:290) at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter$SavedState.(AbstractSlideExpandableListAdapter.java:279) at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter$SavedState$1.createFromParcel(AbstractSlideExpandableListAdapter.java:304) at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter$SavedState$1.createFromParcel(AbstractSlideExpandableListAdapter.java:302) at android.os.Parcel.readParcelable(Parcel.java:2103) at android.os.Parcel.readValue(Parcel.java:1965) at android.os.Parcel.readSparseArrayInternal(Parcel.java:2255) at android.os.Parcel.readSparseArray(Parcel.java:1687) at android.os.Parcel.readValue(Parcel.java:2022) at android.os.Parcel.readMapInternal(Parcel.java:2226) at android.os.Bundle.unparcel(Bundle.java:223) at android.os.Bundle.getSparseParcelableArray(Bundle.java:1232) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:845) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070) at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1856) at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:210) at org.MAW.yourclubfree.Activity_Base.onCreate(Activity_Base.java:40) at android.app.Activity.performCreate(Activity.java:5047) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045) ... 11 more

josejuansanchez commented 11 years ago

Hi,

I got a null pointer exception at the same line (298 in AbstractSlideExpandableListAdapter.writeBitSet) and in the same situations. Has anyone solved this problem?

Thanks!

marcorighini commented 11 years ago

At the moment this bug has not been fixed. I hope it will be soon.

tjerkw commented 11 years ago

There are two separate issues reported here. Both throw an NullPointerException. But the cause is different. One ise caused by using v4 support library. It has an easy fix.

The other issue reported by @CandidoCaballero is caused by the fact no adapter was set on the ListView. Is this true @CandidoCaballero

marcorighini commented 11 years ago

What's the easy fix for the first problem, the one related to v4 support library?

CandidoCaballero commented 11 years ago

Hello,

I´m not sure if I´m doing something wrong,

this is the call ActionSlideExpandableListView ExpandableListView; ExpandableListView = (ActionSlideExpandableListView) findViewById(R.id.paired_devices); MyCustomAdapter mPairedDevicesArrayAdapterNew = new MyCustomAdapter( context, R.layout.expandable_list_item, mItems, activity); ExpandableListView.setAdapter(mPairedDevicesArrayAdapterNew);

This is the adapter

public class MyCustomAdapter extends ArrayAdapter {
Activity activity; String log="MyCustomAdapter";

public MyCustomAdapter(Context context, int textViewResourceId, String[] objects, Activity activity) { super(context, textViewResourceId, objects); this.activity=activity; }

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    try {
        // Inflate the layout, mainlvitem.xml, in each row.
        LayoutInflater inflater = activity.getLayoutInflater();
        View row = inflater.inflate(R.layout.expandable_list_item, parent,
                false);

        // Declare and define the TextView, "item." This is where
        // the name of each item will appear.
        TextView item = (TextView) row.findViewById(R.id.title);
        item.setText(ListDevicesBTActivity.mItems[position]);

        TextView desc = (TextView) row.findViewById(R.id.desc);
        desc.setText(ListDevicesBTActivity.mDescs[position]);

        // Declare and define the TextView, "icon." This is where the icon
        // in each row will appear.
        ImageView icon = (ImageView) row.findViewById(R.id.icon);
        icon.setBackgroundDrawable(ListDevicesBTActivity.mIcons[position]);

        ImageView icon2 = (ImageView) row.findViewById(R.id.icon2);
        icon2.setBackgroundDrawable(ListDevicesBTActivity.mIcons2[position]);

        ImageView sound = (ImageView) row.findViewById(R.id.sound);
        if (ListDevicesBTActivity.mSound[position] != null
                && ListDevicesBTActivity.mSound[position] == true)
            sound.setVisibility(ImageView.VISIBLE);
        else {
            sound.setVisibility(ImageView.GONE);
        }

        ImageView deviceIcon = (ImageView) row.findViewById(R.id.device);
        deviceIcon.setBackgroundResource(R.drawable.bluetooth);

        return row;
    } catch (Exception e) {
        String err = (e.getMessage() == null) ? "error"
                : e.getMessage();
        Log.e(log, err);
        return null;
    }
}

}

JesseFarebro commented 11 years ago

@CandidoCaballero a couple things I notice, please don't call the instance of the ActionSlideExpandableListView ExpandableListView, it gets extremely confusing as there is a core API view called ExpandableListView.

Anyways you are improperly setting the adapter. You have to use the SlideExpandableListAdapter to wrap your adapter or it will throw an NPE. Edit: My mistake it just wraps it for you, never looked at the implementation of ActionSlideExpandableListView

CandidoCaballero commented 11 years ago

Hello JesseFarebro, I Implemented the adapter as you say but the problem is still there.

Any other idea about what is the problem? Thank you very much for your help.

marcorighini commented 11 years ago

@tjerkw no news for fixing the first problem?

tjerkw commented 11 years ago

@marcorighini when you have a fix for the problem please submit a patch, i'm happy to accept it.

This is an open source project so I gladly accept fixes.

Currently I dont have the time to work on these issues. Thats why i opened this project to a lot more contributers.

marcorighini commented 11 years ago

@tjerkw Sorry, i've understood that the fix was really easy so I asked you :)

If I'll fix it, I'll release the patch.

dabluck commented 11 years ago

@marcorighini I'm dealing with this currently. Did you resolve it in your project?

DerSchimi commented 11 years ago

Hey. I am also facing ths problem. Can anybody give me a hint how to fix this? Thanks.

satyadeepk commented 11 years ago

I am also facing this issue, any leads on fixing this please?

dabluck commented 11 years ago

We just used the blog post that inspired the library to write our own custom solution rather than spend more time trying to debug this. http://udinic.wordpress.com/2011/09/03/expanding-listview-items/

tompaperbagltd commented 10 years ago

A dirty fix for this problem is to do a check to see if the list adapter is empty. If it is create a test list item and populate the list with it, then set the list visibility to gone.

chemouna commented 10 years ago

a possible solution is to simply initialize openItems in SavedState with an instance of BitSet instead of null : In SavedState (in AbstractSlideExoandableListAdapter#310) : public BitSet openItems = new BitSet();

satyadeepk commented 10 years ago

@chemouna, We have tried that. But in that case, a different exception occurs. It still crashes, but in a different function.

Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@428f0640: Unmarshalling unknown type code 7209057 at offset 604
       at android.os.Parcel.readValue(Parcel.java:2038)
       at android.os.Parcel.readMapInternal(Parcel.java:2254)
       at android.os.Bundle.unparcel(Bundle.java:223)
       at android.os.Bundle.getSparseParcelableArray(Bundle.java:1237)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:861)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
       at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1872)
       at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:215)
       at com.zoylab.keepintouch.ContactManager.onCreate(ContactManager.java:58)
       at android.app.Activity.performCreate(Activity.java:5369)
chemouna commented 10 years ago

@satyadeepk you're right , i dont know if this is the same problem as yours but somehow in my SavedState implementations of SavedState(Parcel in) and writeToParcel(Parcel out, int flags) was somehow reversed, instead it should be :

 private SavedState(Parcel in) {
        super(in);
        lastOpenPosition = in.readInt();
        openItems = readBitSet(in);
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        super.writeToParcel(out, flags);
        out.writeInt(lastOpenPosition);
        writeBitSet(out, openItems);
    }
satyadeepk commented 10 years ago

@chemouna, Did you try with reversing the SavedState(Parcel in) and writeToParcel(Parcel out, int flags) as you mentioned above?

ahmadalinasir commented 10 years ago

Hi,

I had the same issue. I was using Nested Fragments inside a ViewPager. I simply used 2 things and the issue was fixed. 1) extended my PagerAdapter by FragmentStatePagerAdapter instead of just FragmentPagerAdapter 2) and in my parent fragment I just added this mViewPager.setOffscreenPageLimit(4); //4 is the number of nested fragments.

FYI my app architecture is NavigationDrawer Fragent [ViewPager] Fragment_A [SlideExpandableListView] Fragment_B [SlideExpandableListView] Fragment_C [SlideExpandableListView] Fragment_D [SlideExpandableListView]

Hope it helps someone...

wrf566 commented 10 years ago

how to fix this error ? who can tell me please

tjerkw commented 10 years ago

I commited some code which should prevent the crash. I'm not sure why we got a nullpointerexception but at least it should be prevented now. I probably didnt resolve the root issue.. But can anybody confirm wether its is working.

tompaperbagltd commented 10 years ago

It seems to work on mine now, Thanks!

hamzaawwad commented 10 years ago

how to fix this bug?

tjerkw commented 10 years ago

@hamzaawwad its already fixed. If not please try fixing it yourself and provide a pull request.

hamzaawwad commented 10 years ago

yes its fixed I updated the library thanks alot