sephiroth74 / HorizontalVariableListView

Horizontal list view for Android which allows variable items widths
859 stars 283 forks source link

Long press on items is throwing NullPointerExceptions #71

Closed saket closed 1 year ago

saket commented 10 years ago

First of all, thank you so much for this fantastic custom ListView.

Everything is working perfectly, except for one thing. Long-pressing on a child is throwing NullPointerExceptions. Here's the stack-trace:

java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.view.ActionMode$Callback.onCreateActionMode(android.view.ActionMode, android.view.Menu)' on a null object reference
            at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionMode(PhoneWindow.java:2678)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionModeForChild(PhoneWindow.java:2620)
            at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:665)
            at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:665)
            at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:665)
            at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:665)
            at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:665)
            at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:665)
            at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:665)
            at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:665)
            at android.view.View.startActionMode(View.java:4536)
            at it.sephiroth.android.library.widget.AbsHListView.performLongPress(AbsHListView.java:2572)
            at it.sephiroth.android.library.widget.AbsHListView$CheckForLongPress.run(AbsHListView.java:2529)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

The issue is happening at line #2572 of AbsHListView.java.

Current workaround: By setting the choice mode of HListView to anything other than ListView.CHOICE_MODE_MULTIPLE_MODAL so that the line where the NPE is being thrown is never reached.

matt18224 commented 10 years ago

This appears to be a result of setting the selection mode as ListView.CHOICE_MODE_MULTIPLE_MODAL without registering a MultiChoiceModeListener with a call to AbsHListView.setMultiChoiceModeListener(). The code seems to suggest that an IllegalArgumentException should be thrown with a message indicating that a callback is required, but the condition preceding its throwing evaluates to false and a wrapper of a null callback gets passed up the view hierarchy until it's called, at which point it throws a NullPointerException. An easy fix is to simply register a callback.

public void setItemChecked( int position, boolean value ) {
        if ( mChoiceMode == ListView.CHOICE_MODE_NONE ) {
            return;
        }

        // Start selection mode if needed. We don't need to if we're unchecking something.
        if ( android.os.Build.VERSION.SDK_INT >= 11 ) {
            if ( value && mChoiceMode == ListView.CHOICE_MODE_MULTIPLE_MODAL && mChoiceActionMode == null ) {
                if ( mMultiChoiceModeCallback == null ||
                        !((MultiChoiceModeWrapper)mMultiChoiceModeCallback).hasWrappedCallback() ) {
                    throw new IllegalStateException( "AbsListView: attempted to start selection mode " +
                            "for CHOICE_MODE_MULTIPLE_MODAL but no choice mode callback was " +
                            "supplied. Call setMultiChoiceModeListener to set a callback." );
                }
                mChoiceActionMode = startActionMode( (MultiChoiceModeWrapper)mMultiChoiceModeCallback );
            }
        }

...
saket commented 10 years ago

The selection mode is set ListView.CHOICE_MODE_MULTIPLE_MODAL by default. I think this should be removed because the developer isn't aware of this.

cyberrob-zz commented 9 years ago

you could just set the HListView ListView.CHOICE_MODE_SINGLE to skip the exception.