Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwipeListView usage NullPointerException

I'm trying to use SwipeListView, but when I touch an item on my app, it's will be crash, the log info is there:

05-26 21:52:26.545: E/AndroidRuntime(19862): java.lang.NullPointerException
05-26 21:52:26.545: E/AndroidRuntime(19862): at com.fortysevendeg.android.swipelistview.SwipeListViewTouchListener.setFrontView(SwipeListViewTouchListener.java:121)
05-26 21:52:26.545: E/AndroidRuntime(19862): at com.fortysevendeg.android.swipelistview.SwipeListViewTouchListener.onTouch(SwipeListViewTouchListener.java:485)
05-26 21:52:26.545: E/AndroidRuntime(19862): at com.fortysevendeg.android.swipelistview.SwipeListView.onInterceptTouchEvent(SwipeListView.java:481)

I use it step by step

import android.app.Activity;
import android.os.Bundle;
import com.fortysevendeg.android.swipelistview.SwipeListView;
import com.mkyong.android.adaptor.MobileArrayAdapter;

public class ListMobileActivity extends Activity {

    static final String[] MOBILE_OS = new String[] { "Android", "iOS",
            "WindowsMobile", "Blackberry"};

    MobileArrayAdapter myad ;
    private SwipeListView swipeListView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainactivity);  
        swipeListView =(SwipeListView)findViewById(R.id.book_listview);
        myad=new MobileArrayAdapter(this, MOBILE_OS);
        swipeListView.setAdapter(myad); 

    }

}

Here is my Layout file

<?xml version="1.0" encoding="UTF-8"?>
android:id="@+id/LinearLayoutListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >

xmlns:swipe="http://schemas.android.com/apk/res-auto"
android:id="@+id/book_listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="#00000000"
swipe:swipeActionLeft="dismiss"
swipe:swipeBackView="@+id/back"
swipe:swipeCloseAllItemsWhenMoveList="true"
swipe:swipeFrontView="@+id/front"
swipe:swipeMode="both" />

Did there anything I miss? Thanks

like image 713
wlyzlx Avatar asked Dec 02 '22 23:12

wlyzlx


1 Answers

Just ensure that layout you are inflating in you adapter contains @+id/back and @+id/front components.

In other words, if your SwipeListView declaration declares @+id/back and @+id/front as swipe views ...

    <...SwipeListView
        android:id="@+id/swipe_list"
        android:layout_width="match_parent"
        android:layout_weight="1.0"
        android:layout_height="0dp"
        app:swipeFrontView="@+id/front" <=====
        app:swipeBackView="@+id/back" <=====
        app:swipeActionLeft="dismiss"
        app:swipeCloseAllItemsWhenMoveList="true"
        app:swipeMode="both"
    />

... then it is expected that layout which you inflate in your adapter contains these components. In example the layout being inflated in adapter is called package_row.xml, here is a snipped of adapter:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final PackageItem item = getItem(position);
    ViewHolder holder;
    if (convertView == null) {
        LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = li.inflate(R.layout.package_row, parent, false);
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        holder = new ViewHolder();
        holder.ivImage = (ImageView) convertView.findViewById(R.id.example_row_iv_image);
        ...
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    ...
like image 112
Michal Vician Avatar answered Dec 12 '22 22:12

Michal Vician