NullPointerException when enabling list-and-drag from OnItemLongClickListener as a list

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

Things work when I enable dragsort when creating. However, I would like to leave it disabled until a long click on one of the list items.

Therefore, I would like to establish

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; } }); 

I hope that a long click on any item will enable the DSLV and allow it to be deleted or deleted, after which the DSLV will be disabled again.

However, after (not yet) 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 seems like one of the NULL motion events is probably because I enable DSLV during the touch event.

+6
source share
2 answers

I am facing the same problem - could you get around this?

One solution was to set the drag handler on the list item in GONE initially - this way the user cannot drag it to sort it. When long press the triggers, set the drag visibility to go to VISIBLE. However, the problem you are facing is that when you deselect an item after a long press, even if you set the drag handler to GONE again, the user can still drag the sort by clicking on the position where the handler used to be.

+1
source

You can use the dslv: drag_start_mode attribute in your XML layout. eg:

 <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" /> 
0
source

Source: https://habr.com/ru/post/953030/


All Articles