Swipelistview不起作用

时间:2013-10-28 14:19:59

标签: android listview swipe ontouchlistener swipe-gesture

我正在尝试在我的项目中使用Swipelistview,但它会抛出NullPointer说:

java.lang.NullPointerException
at com.fortysevendeg.swipelistview.SwipeListViewTouchListener.setFrontView(SwipeListViewTouchListener.java:138)
at com.fortysevendeg.swipelistview.SwipeListViewTouchListener.onTouch(SwipeListViewTouchListener.java:731)
at com.fortysevendeg.swipelistview.SwipeListView.onInterceptTouchEvent(SwipeListView.java:630)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1852)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)   

在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910) 在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)

我已按以下方式实施了该库:

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

    <com.fortysevendeg.swipelistview.SwipeListView
        xmlns:swipe="http://schemas.android.com/apk/res-auto"
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="35dp"
        android:layout_marginTop="51dp"
        android:listSelector="#00000000"
        swipe:swipeActionLeft="dismiss"
        swipe:swipeActionRight="dismiss"
        swipe:swipeAnimationTime="1000"
        swipe:swipeBackView="@+id/back"
        swipe:swipeCloseAllItemsWhenMoveList="false"
        swipe:swipeFrontView="@+id/front"
        swipe:swipeMode="both"
        swipe:swipeOffsetLeft="20dp"
        swipe:swipeOffsetRight="20dp"
        swipe:swipeOpenOnLongPress="false" />  

</LinearLayout>

这是代码:

private SwipeListView listview1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        States states_name[] = new States[] {
                new States("Item 1", "Item A"),
                new States("Item 2", "Item B"),
                new States("Item 3", "Item C"),
                new States("Item 4", "Item D"),
                new States("Item 5", "Item E"),
                new States("Item 6", "Item F"), 
                new States("Item 7", "Item G"),
                new States("Item 8", "Item H")
        };

        StateAdapter adapter = new StateAdapter(this, R.layout.listtoinflate, states_name);

        listview1 = (SwipeListView)findViewById(R.id.listView1);
     listview1.setAdapter(adapter);


    }

2 个答案:

答案 0 :(得分:0)

不应该......

States states_name[] = new States[]
是吗?......

States[] states_name = new States[]

答案 1 :(得分:0)

您可能缺少ID为“front”和“back”的元素,如您在布局中的com.fortysevendeg.swipelistview.SwipeListView实现中指定的那样。它们应该在listview行布局中,在你的情况下应该是你的适配器中的“R.layout.listtoinflate”。

这里有一些示例代码: https://github.com/47deg/android-swipelistview-sample/blob/master/res/layout/package_row.xml

此问题也在此主题中得到解答: SwipeListView usage NullPointerException