简体   繁体   中英

NullPointerException when enabling drag-sort-listview from a OnItemLongClickListener in a listview

I am using drag-sort-listview ( https://github.com/bauerca/drag-sort-listview ) in a ListFragment.

Things are working when I enable the dragsort on creation. However, I would like to leave it disabled until a longclick on one of the items in the listview.

So I would like to set

mDslv.setDragEnabled(false);

in 3 places: onCreateView(), DragSortListView.DropListener, and DragSortListView.RemoveListener

In my ListFragment, I have:

        listView.setOnItemLongClickListener( new AdapterView.OnItemLongClickListener(){ 
            @Override 
            public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
                mDslv.setDragEnabled(true);
                return true; 
            } 
        });

My hope is that a long click on any item will turn on the DSLV and allow that item to be dropped or removed, after which DSLV will be disabled again.

However, after (not while) the setDrageEnabled(true) is called, I get a NullPointerException:

09-03 00:03:33.749: E/AndroidRuntime(9703): FATAL EXCEPTION: main
09-03 00:03:33.749: E/AndroidRuntime(9703): java.lang.NullPointerException
09-03 00:03:33.749: E/AndroidRuntime(9703):     at com.mobeta.android.dslv.DragSortController.onScroll(DragSortController.java:381)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at android.view.GestureDetector.onTouchEvent(GestureDetector.java:541)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at com.mobeta.android.dslv.DragSortController.onTouch(DragSortController.java:243)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at android.view.View.dispatchTouchEvent(View.java:3881)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1703)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1133)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1687)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2196)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1880)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at android.os.Looper.loop(Looper.java:130)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at android.app.ActivityThread.main(ActivityThread.java:3729)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at java.lang.reflect.Method.invokeNative(Native Method)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at java.lang.reflect.Method.invoke(Method.java:507)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
09-03 00:03:33.749: E/AndroidRuntime(9703):     at dalvik.system.NativeStart.main(Native Method)

Has anyone tried this? Any ideas? It looks like one of the motion events is NULL, probably because I am enabling DSLV during a touch event.

I am hitting the same issue - were you able to work around this?

One solution I had was to set the drag handler on the list view item to GONE initially - this way the user can't drag to sort. When the long press triggers, set the drag handler's visibility to VISIBLE. However a problem a ran into here is that when you de-select the item after long pressing it, even if you set the drag handler to GONE again, the user can still drag sort by pressing on the position where the handler used to be.

You can use "dslv:drag_start_mode" attribute in your layout XML. like this:

<com.mobeta.android.dslv.DragSortListView
    xmlns:dslv="http://schemas.android.com/apk/res/com.yourdomain.test"
    android:id="@+id/custom_ticker_list"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:dividerHeight="1dp"
    android:childDivider="@color/line"
    dslv:drag_start_mode="onLongPress"
    />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM