0

I came across a crash in Android 3.2 when trying to test out Multi-touching with a list view. I can reproduce the exception with a simple layout containing only a ListView and less than 20 lines of Java code. Anyone's advice on the matter would be greatly appreciated.

What I'm trying to do:

I want to have the users be able to check many items on the ListView by dragging without getting in the way of scrolling the ListView, so I figured users would use one finger to scroll the ListView, and click and drag with two fingers to select items they move over. Naturally I decided to set an onTouchListener of my ListView in order to process the multi-touch event.

The Issue: Scrolling with one finger works perfectly. But when I touch with two fingers and then lift up, my app (sometimes) crashes immediately. Sometimes meaning around 70% of the time, which leads me to believe it has to do with going from 2 fingers to 0 fingers. The crash isn't even in any code that I own, but instead in a native method in the MotionEvent called by ListView itself (pastebin of full stack trace so that I don't clutter the page).

The code: Here's what I wrote in order to cause the problem.

MainActivity.java

public class MainActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView lv = ((ListView) findViewById(R.id.listView1));

        String[] list = new String[]{"a", "b", "c", "d", "e", "f", "g"};

        lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));
        lv.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getPointerCount() != 2) {
                    return false;
                }
                return true;
            }
        });
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

    </ListView>

</RelativeLayout>

And that's it. I'm running my program on a Thrive running Android 3.2.1 and my project targets API level 13 (3.2). I don't have another tablet to test it on and I can't use my normal dev virtual machine because there's no multi-touch support.

If I call the getter and return either false or true regardless of the value it doesn't crash. I'm thinking that it has to do with the fact that the ListView gets a click event when I do the touch, and then when my listener is called it starts consuming any events that contain two fingers. Then when I lift both my fingers off simultaneously there's no event passed through the ListView saying it's done, so it still expects to be able to find a pointer index for the selected item.

So my question is "Did what I write break some sort of contract that ListView has?" Should I be doing this testing some other way?

Also, if someone could point me to the source for API level 13 I'd be very appreciative. It isn't accessible through the Android SDK manager (only 14-16 are) and if I had it I'd more likely be able to understand the root of the issue.

Carrotman42
  • 1,744
  • 1
  • 14
  • 19
  • Check this: http://stackoverflow.com/questions/6919292/pointerindex-out-of-range-android-multitouch – Comic Sans MS Lover Jul 06 '12 at 14:05
  • That has to do with code that they actually wrote though. In the stack trace I provided it shows that the crash is not directly caused by my code (my package is com.ksoft.sandbox). – Carrotman42 Jul 06 '12 at 14:13

0 Answers0