1

Hello stackoverflow,

I have a ListView on my page - problem is it crashes each time i try to scroll it in some direction. It is not a regular ListView, I am using a github library, that can be found here: https://github.com/terlici/DragNDropList

Here is the setup of my list:

DragNDropListView lv = (DragNDropListView) getListView();
DragNDropSimpleAdapter adapter = new DragNDropSimpleAdapter(this, listItems, R.layout.custom_row_view, new String[]{"name", "current", "reset"},  new int[] {R.id.text1, R.id.text2, R.id.text3}, R.id.handler);
lv.setDragNDropAdapter(adapter);

UPDATED LOGACT

03-09 14:48:43.667: E/AndroidRuntime(18869): FATAL EXCEPTION: main
03-09 14:48:43.667: E/AndroidRuntime(18869): java.lang.ArrayIndexOutOfBoundsException
03-09 14:48:43.667: E/AndroidRuntime(18869):    at com.terlici.dragndroplist.DragNDropSimpleAdapter.getView(DragNDropSimpleAdapter.java:66)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.widget.AbsListView.obtainView(AbsListView.java:1409)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.widget.ListView.makeAndAddView(ListView.java:1745)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.widget.ListView.fillDown(ListView.java:670)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.widget.ListView.fillGap(ListView.java:641)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.widget.AbsListView.trackMotionScroll(AbsListView.java:3399)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.widget.AbsListView.onTouchEvent(AbsListView.java:2233)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.widget.ListView.onTouchEvent(ListView.java:3446)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at com.terlici.dragndroplist.DragNDropListView.onTouchEvent(DragNDropListView.java:116)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.view.View.dispatchTouchEvent(View.java:3885)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.os.Looper.loop(Looper.java:130)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at android.app.ActivityThread.main(ActivityThread.java:3683)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at java.lang.reflect.Method.invokeNative(Native Method)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at java.lang.reflect.Method.invoke(Method.java:507)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-09 14:48:43.667: E/AndroidRuntime(18869):    at dalvik.system.NativeStart.main(Native Method)
03-09 14:48:48.897: I/Process(18869): Sending signal. PID: 18869 SIG: 9

The class from the lib that causes this crash (DragNDropSimpleAdapter line 66): ` public class DragNDropSimpleAdapter extends SimpleAdapter implements DragNDropAdapter { int mPosition[]; int mHandler;

    public DragNDropSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to, int handler) {
        super(context, data, resource, from, to);

        mHandler = handler;
        setup(data.size());
    }

    private void setup(int size) {
        mPosition = new int[size];

        for (int i = 0; i < size; ++i) mPosition[i] = i;
    }

    @Override
    public View getDropDownView(int position, View view, ViewGroup group) {
        return super.getDropDownView(mPosition[position], view, group);
    }

    @Override
    public Object getItem(int position) {
        return super.getItem(mPosition[position]);
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(mPosition[position]);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(mPosition[position]);
    }

    @Override
    public View getView(int position, View view, ViewGroup group) {
        return super.getView(mPosition[position], view, group);
    }

    @Override
    public boolean isEnabled(int position) {
        return super.isEnabled(mPosition[position]);
    }

    @Override
    public void onItemDrag(DragNDropListView parent, View view, int position, long id) {

    }

    @Override
    public void onItemDrop(DragNDropListView parent, View view, int startPosition, int endPosition, long id) {
        int position = mPosition[startPosition];

        if (startPosition < endPosition)
            for(int i = startPosition; i < endPosition; ++i)
                mPosition[i] = mPosition[i + 1];
        else if (endPosition < startPosition)
            for(int i = startPosition; i > endPosition; --i)
                mPosition[i] = mPosition[i - 1];

        mPosition[endPosition] = position;
    }

    @Override
    public int getDragHandler() {
        return mHandler;
    }
}

My XML Code

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

    <LinearLayout
        android:id="@+id/profile_and_reset"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/relativelayout"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/profileView"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.94"
            android:layout_gravity="center_vertical"
            android:textSize="18sp"/>

        <Button
            android:id="@+id/reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="@string/reset_button" />
    </LinearLayout>

<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"
    android:layout_below="@+id/profile_and_reset"
    android:id="@+id/seperator"/>

    <com.terlici.dragndroplist.DragNDropListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/seperator"
        android:layout_above="@+id/second_seperator"/>

<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"
    android:id="@+id/second_seperator"
    android:layout_above="@+id/add_and_instructions"/>

    <LinearLayout
        android:id="@+id/add_and_instructions"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/relativelayout"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

    <Button
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="@string/add_button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/instructions" />

    </LinearLayout>

</RelativeLayout>

UPDATE

now that i switched to a regular activity, this method isn't working:

@SuppressWarnings("unchecked")
protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        if(lv.isClickable()){
            super.onListItemClick(l, v, position, id);
            Map<String, String> map = (Map<String, String>) lv.getAdapter().getItem(position);
            String scurrents = map.get("current");
            String getnames = map.get("name");
            String getname = java.net.URLEncoder.encode(getnames);

            int current = Integer.parseInt(scurrents);
            if(current>0){
                int newcurrent = current-1;

                SetSql UC = new SetSql(this);
                UC.open();
                UC.changeCurrent(newcurrent, current, getname);
                UC.close();
                onResume();
            }
        }
}

I am getting an error in the super.onListItemClick, since it is not applicable for an Activity. How can i solve this?

rel-s
  • 6,108
  • 11
  • 38
  • 50

2 Answers2

0

You are running into the error as the getView() is trying to access an array out of bounds, which I believe is due to the listView not being referenced properly to be attached to the adapter.

DragNDropListView lv = (DragNDropListView)findViewById(R.id.list);

Swayam
  • 16,294
  • 14
  • 64
  • 102
  • When removing the `android.R.id.list` and replacing it with `R.id.list` I get the following error: `list can not be resolved or is not a field` – rel-s Mar 09 '13 at 11:27
  • In that case you need to **clean and build** your project. Do not use android.R even if Eclipse suggests you to do so. Perform clean and build. Also make sure that your xml files do not have any errors. – Swayam Mar 09 '13 at 11:28
  • In my XML file, that listview id is setup like this: `android:id="@android:id/list"` Is this ok? – rel-s Mar 09 '13 at 11:29
  • Change it to `android:id="@id/list"` – Swayam Mar 09 '13 at 11:32
  • This didn't work, but when i changed it to `android:id="@+id/list" it didn't throw an error, but when starting the app it crashed telling me i have an error in the line setting the ContentView to that XML file. What is wrong here? – rel-s Mar 09 '13 at 11:35
  • I am sorry. I actually meant @+id only. Silly typo. My bad. Sorry. And what exactly are you getting in the LogCat ? – Swayam Mar 09 '13 at 11:36
  • My logcat is posted above, and it says that i am using `android.R.id.list` somewhere, while I am not :O – rel-s Mar 09 '13 at 11:41
  • Might be confusing, but i updated my logcat in the same place it was earlier. – rel-s Mar 09 '13 at 11:44
  • Did you change your fndViewById to R.id.list ? – Swayam Mar 09 '13 at 11:46
  • Whoops, my logcat said that my content **MUST** have a `ListView` with the id of `android.R.id.list`. Is that normal? – rel-s Mar 09 '13 at 11:50
  • Use `ArrayAdapter` instead of `SimpleAdapter` – mihail Mar 09 '13 at 11:53
  • Yeah, I saw that just now. I think you must be using a ListActivity ? – Swayam Mar 09 '13 at 11:53
  • @mihali Why should I? Simple adapter worked perfectly until i used this library, so thats not the problem. – rel-s Mar 09 '13 at 11:54
  • @swayam I am, should i change it to a regular activity? – rel-s Mar 09 '13 at 11:55
  • Since you are using ListActivity, you can just use `getListView()` instead of findViewById(..). But make sure to change your id back to `android:id="@android:id/list"`. Actually, you never said that you are using ListActivity. Anyway, set the id back to `android:id="@android:id/list"` and use getListView() instead of findViewById(). – Swayam Mar 09 '13 at 12:01
  • Have a look at http://stackoverflow.com/questions/3040374/runtime-exception-listview-whose-id-attribute-is-android-r-id-list and http://stackoverflow.com/questions/8696506/listview-error-your-content-must-have-a-listview-whose-id-attribute-is-androi to understand ListView id properly. – Swayam Mar 09 '13 at 12:02
  • @swayam So i did this, and now i am getting again the error about scrolling :( – rel-s Mar 09 '13 at 12:05
  • That is really sad! Maybe it is an issue with the library that you are using, because this usually have always worked for me. Bdw, I hope you used `DragNDropListView lv = getListView()`. – Swayam Mar 09 '13 at 12:15
  • This would be terrible if there is a problem in the library.. Do you have any other libraries for this? And I had to cast it like this `DragNDropListView lv = (DragNDropListView) getListView();` – rel-s Mar 09 '13 at 12:47
  • Not at all, still throwing me that error.. I can't seem to figure this out! It says there is an array out of bounds exception in the `getView()` method, but why would that happen? I looked at that code and it seemed really OK to me. – rel-s Mar 09 '13 at 12:54
  • Well, even I am out of ideas right now. One last thing, could you post your xml please ? – Swayam Mar 09 '13 at 13:02
  • No problem. +1'd you just now for all the effort you are putting! :) And well, I dont know much about the DragNDropList, but in the Java code you are using R.text1, R.text2, etc, but I couldn't see anything like R.text1, R.text2 in your XML. Are you sure it is correct ? – Swayam Mar 09 '13 at 13:14
  • @swayam The r.text1 and text2 etc are in a different XML file, they are in the custom_row_view.xml - Do you want me to post it as well? – rel-s Mar 09 '13 at 13:18
  • No no. Nevermind. I cant seriously figure out what might have gone wrong. Do you have any sample code that uses this library ? – Swayam Mar 09 '13 at 13:21
  • This sample app is using a regular Activity like I asked you to. Maybe you should follow this sample app code and do exactly how they have done. And, now you see why I prefer regular activities ? ;) – Swayam Mar 09 '13 at 13:29
  • @swayam I switched to a regular activity, but now I can't use my `onListItemClick` method. The methods code is posted above in my thread. – rel-s Mar 09 '13 at 13:38
  • I dont understand your question. you would need to register your ListView with an onClickHandler, just like you do for a button. – Swayam Mar 09 '13 at 13:50
  • Indeed, but how do i pass all the arguments like i did with my previous method? – rel-s Mar 09 '13 at 13:59
  • @Swayam ok so the onitemclick is set and working, but still the scrolling isn't working.. I have no idea what to do! – rel-s Mar 09 '13 at 14:10
  • @swayam i have now noticed that i am capable of scrolling down until a specific point, and it always crashes there, maybe that helps? – rel-s Mar 09 '13 at 14:13
  • Well, I have no idea either. All I can suggest you to do is follow the sample code, line by line. – Swayam Mar 09 '13 at 14:13
  • Well, in that case, it means that everything is working fine until the code is trying to access an array that is going out of bounds. The array is not being terminated properly. I guess you could run it in the debug mode and check the values of the variables when the crash takes place. – Swayam Mar 09 '13 at 14:25
  • 1
    @swayam So i did some debugging, and found out some interesting things.. First, there seems to be no problem in my code, it works fine. But when setting a breakpoint to the problematic line in the library (Line 66), I saw that the `position` variable slowly rose up until it exceeded that amount of elements in mPosition. Right now i am trying to disable this problem by making sure this could never happen using if statements. – rel-s Mar 09 '13 at 15:41
  • Actually, this is not possible.. Maybe there is another way to make sure this wont happen? Do you see one? – rel-s Mar 09 '13 at 15:44
  • 1
    @swayam I SOLVED THE BUG! It was just because my declaration of the DragNDropListView variable was in a loop, so that caused bugs. Thank you so much for your kind help! – rel-s Mar 09 '13 at 15:48
0

As shown in your Logcat file:

you have to have the id of your listview setted to be

android.R.id.list
Husam A. Al-ahmadi
  • 2,056
  • 2
  • 20
  • 27
  • This was fixed, please take a look at my code in the post. However, it is still not working (Take a look at the logcat as well, it was updated too) – rel-s Mar 09 '13 at 12:58