Listview在滚动时崩溃

时间:2013-03-09 11:15:37

标签: android drag-and-drop android-listview indexoutofboundsexception simpleadapter

Hello stackoverflow,

我的页面上有一个ListView - 问题是每次我尝试向某个方向滚动它时都会崩溃。它不是常规的ListView,我使用的是github库,可以在这里找到: https://github.com/terlici/DragNDropList

以下是我的清单设置:

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

更新后的记录

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

lib中导致此崩溃的类(DragNDropSimpleAdapter第66行): `     公共类DragNDropSimpleAdapter扩展SimpleAdapter实现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;
    }
}

我的XML代码

<?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>

更新

现在我切换到常规活动,此方法无效:

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

我在super.onListItemClick收到错误,因为它不适用于活动。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

你正在遇到错误,因为getView()试图访问一个超出界限的数组,我认为这是因为listView没有被正确引用以连接到适配器。

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

答案 1 :(得分:0)

如Logcat文件中所示:

您必须将listview的ID设置为

android.R.id.list