bauerca / drag-sort-listview

Android ListView with drag and drop reordering.
3.2k stars 1.44k forks source link

NPE when scrolling #77

Closed theboubougne closed 11 years ago

theboubougne commented 11 years ago

Hi,

My row layout is composed of an horizontal layout which contained an imageView (drag handle), a second imageview and then some text.

When I open my listview I can scroll fine using the textview. If I hit the handle I can drag and drog the row. But if I try to scroll using the second imageview I got a NPE, but only if it's the 1st action after opening this activity. For example if I scroll using the textview, I can then scroll using the 2nd ImageView without any problem... FYI I'm using the latest code.

Here's the listview layout <com.mobeta.android.dslv.DragSortListView xmlns:dslv="http://schemas.android.com/apk/res/com.test" android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:divider="@drawable/gradient_divider" android:dividerHeight="1dp" android:focusable="true" android:scrollbars="vertical" dslv:collapsed_height="2dp" dslv:drag_enabled="true" dslv:drag_handle_id="@+id/grabber" dslv:drag_scroll_start="0.33" dslv:drag_start_mode="onDown" dslv:float_alpha="0.6" dslv:max_drag_scroll_speed="0.5" dslv:slide_shuffle_speed="0.3" dslv:sort_enabled="true" dslv:track_drag_sort="false" dslv:use_default_controller="true" />

Heres the stack: 02-11 22:34:33.228: E/AndroidRuntime(25486): java.lang.NullPointerException 02-11 22:34:33.228: E/AndroidRuntime(25486): at com.mobeta.android.dslv.DragSortController.onScroll(DragSortController.java:382) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.GestureDetector.onTouchEvent(GestureDetector.java:572) 02-11 22:34:33.228: E/AndroidRuntime(25486): at com.mobeta.android.dslv.DragSortController.onTouch(DragSortController.java:243) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.View.dispatchTouchEvent(View.java:7234) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 02-11 22:34:33.228: E/AndroidRuntime(25486): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953) 02-11 22:34:33.228: E/AndroidRuntime(25486): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.app.Activity.dispatchTouchEvent(Activity.java:2410) 02-11 22:34:33.228: E/AndroidRuntime(25486): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.View.dispatchPointerEvent(View.java:7419) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:171) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4342) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4382) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.Choreographer.doCallbacks(Choreographer.java:562) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.Choreographer.doFrame(Choreographer.java:530) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.os.Handler.handleCallback(Handler.java:725) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.os.Handler.dispatchMessage(Handler.java:92) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.os.Looper.loop(Looper.java:137) 02-11 22:34:33.228: E/AndroidRuntime(25486): at android.app.ActivityThread.main(ActivityThread.java:5039) 02-11 22:34:33.228: E/AndroidRuntime(25486): at java.lang.reflect.Method.invokeNative(Native Method) 02-11 22:34:33.228: E/AndroidRuntime(25486): at java.lang.reflect.Method.invoke(Method.java:511) 02-11 22:34:33.228: E/AndroidRuntime(25486): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 02-11 22:34:33.228: E/AndroidRuntime(25486): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 02-11 22:34:33.228: E/AndroidRuntime(25486): at dalvik.system.NativeStart.main(Native Method)

bauerca commented 11 years ago

Do you get the same behavior if android:clickable="false"?

theboubougne commented 11 years ago

Yes I still get the NPE On Feb 11, 2013 10:53 PM, "Carl A. Bauer" notifications@github.com wrote:

Do you get the same behavior if android:clickable="false"?

— Reply to this email directly or view it on GitHubhttps://github.com/bauerca/drag-sort-listview/issues/77#issuecomment-13405373.

bauerca commented 11 years ago

This is really weird. Can I see your list item layout as well?

theboubougne commented 11 years ago

The NPE occurs when I start scrolling using the 'thumbnail' imageview

My List layout

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/adViewLayout"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/playListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.myPackage.fragments.PlayListFragment" />

</LinearLayout>

<FrameLayout
    android:id="@+id/adViewLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@android:color/black" />

My ListViewRow layout

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res/com.myPackage" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" >

<ImageView
    android:id="@+id/grabber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:src="@drawable/grabber" />

<ImageView
    android:id="@+id/thumbnail"
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:layout_gravity="center_vertical"
    android:padding="2dp"
    android:scaleType="centerCrop" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:orientation="vertical"
            android:paddingLeft="5dp"
            android:paddingRight="5dp" >

            <TextView
                android:id="@+id/episodeName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/podcastName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:paddingBottom="5dp"
                android:paddingTop="2dp" >

                <TextView
                    android:id="@+id/duration"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:paddingRight="10dp" />

                <com.myPackage.view.PieChart
                    android:id="@+id/positionChart"
                    android:layout_width="16dp"
                    android:layout_height="16dp"
                    android:layout_weight="100"
                    android:background="@null"
                    android:gravity="center_vertical"
                    android:paddingTop="1dp"
                    android:paddingRight="5dp"
                    android:visibility="gone"
                    custom:autoCenterPointerInSlice="false"
                    custom:highlightStrength="1.12"
                    custom:pieRotation="-90"
                    custom:pointerRadius="4dp"
                    custom:showText="false" />

                <ProgressBar
                    android:id="@+id/progressBar"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center_vertical"
                    android:thumb="@android:color/transparent" />
            </LinearLayout>
        </LinearLayout>

        <ImageButton
            android:id="@+id/playButton"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:background="@null"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:src="@drawable/play_button" />
    </LinearLayout>
</RelativeLayout>

bauerca commented 11 years ago

I doubt this is the source of the problem, but your list view row layout_height is match_parent, which should never be the case. Might as well fix that and see what happens...

theboubougne commented 11 years ago

I missed that one... [Edit] The problem is still there

theboubougne commented 11 years ago

It seems that when starting scrolling using the thumbnail I got ev.getAction() == ACTION_MOVE where I get evt.getAction() == ACTION_DOWN when scrolling with the textview

theboubougne commented 11 years ago

I forget to tell you that I had an onClickListener set on the the thumbnail (in the adapter). If I remove it, it works fine. So the problem occurs in case I have a button in the row and I start scrolling using that button

bauerca commented 11 years ago

aha. i'll look into this.

theboubougne commented 11 years ago

Do you have any idea how to prevent those NPE? I have to publish a new version of my app and just need to know if I need to rollback to my previous drag&drop list view component (for now) or if a workaround exists. Thx On Feb 12, 2013 2:32 AM, "Carl A. Bauer" notifications@github.com wrote:

aha. i'll look into this.

— Reply to this email directly or view it on GitHubhttps://github.com/bauerca/drag-sort-listview/issues/77#issuecomment-13413868.

bauerca commented 11 years ago

Going to look at this tomorrow morning. That's 8 hrs from now :)

theboubougne commented 11 years ago

Nice ! Thx a lot On Feb 14, 2013 7:53 AM, "Carl A. Bauer" notifications@github.com wrote:

Going to look at this tomorrow morning. That's 8 hrs from now :)

— Reply to this email directly or view it on GitHubhttps://github.com/bauerca/drag-sort-listview/issues/77#issuecomment-13535528.

bauerca commented 11 years ago

Try this yucky hacky fix. I'll clean it up later.

theboubougne commented 11 years ago

Thanks a lot. It's fixed !

theboubougne commented 11 years ago

I'm not sure if it's related to your fix, but the 1st time I press on a row, onListItemClick() is called but if I press again on the exact same row nothing happens...

theboubougne commented 11 years ago

In fact I still have a NPE in some cases when pressing on a specific ImageButton on my row

02-17 16:07:52.729: E/AndroidRuntime(1469): FATAL EXCEPTION: main 02-17 16:07:52.729: E/AndroidRuntime(1469): java.lang.NullPointerExceptionw² 02-17 16:07:52.729: E/AndroidRuntime(1469): at com.mobeta.android.dslv.DragSortController.onScroll(DragSortController.java:382) 02-17 16:07:52.729: E/AndroidRuntime(1469): at android.view.GestureDetector.onTouchEvent(GestureDetector.java:572) 02-17 16:07:52.729: E/AndroidRuntime(1469): at com.mobeta.android.dslv.DragSortController.onTouch(DragSortController.java:243) 02-17 16:07:52.729: E/AndroidRuntime(1469): at android.view.View.dispatchTouchEvent(View.java:7234) 02-17 16:07:52.729: E/AndroidRuntime(1469): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168) 02-17 16:07:52.729: E/AndroidRuntime(1469): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903) 02-17 16:07:52.729: E/AndroidRuntime(1469): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) 02-17 16:07:52.729: E/AndroidRuntime(1469): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 02-17 16:07:52.729: E/AndroidRuntime(1469): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)

denley commented 11 years ago

Hi everyone. I just had this same issue. I managed to fix it by modifying the onTouch(View v, MotionEvent ev) method in DragSortController. The first few lines of the method are changed as follows:

before:

if (!mDslv.isDragEnabled() || mDslv.listViewIntercepted()) { return false; }

mDetector.onTouchEvent(ev);

after:

mDetector.onTouchEvent(ev); if (!mDslv.isDragEnabled() || mDslv.listViewIntercepted()) { return false; }

The crash occurs because the scrollbar is only getting the MotionEvent.ACTION_MOVE events and not the MotionEvent.ACTION_DOWN event, which is needed to know the point that the scrollbar was first touched. The change makes sure the scrollbar gets all of the events.

shruity commented 8 years ago

I have implemented the dragsortlistview it is working fine even while scrolling but it is giving null pointer exception on long press. I am not able to fix this up. Could anyone please help me with that?

Thanks