开发者

Simple Adapter filtering give null ponter exception

开发者 https://www.devze.com 2023-03-22 23:26 出处:网络
I am trying to create a list view using simple adapter. Here is my code and xml list_main2.xml <?xml version=\"1.0\" encoding=\"utf-8\"?>

I am trying to create a list view using simple adapter. Here is my code and xml

list_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  
xmlns:android="http://schemas.android.com/apk/res/android"  
android:layout_width="fill_parent"  
android:layout_height="wrap_content"  
android:orientation="horizontal"  
android:paddingTop="3dip"  
android:paddingBottom="3dip"  
> 
<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:paddingLeft="2dip"/>

    <LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:orientation="vertical"
    android:paddingLeft="2dip"
    > 

<LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:orientation="horizontal"
    android:paddingLeft="2dip"
    android:weightSum="3.0"
    > 
    <TextView  
        android:id="@+id/caption1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginRight="1dip"   
        android:textSize="15sp"
        android:textStyle="bold"
        android:gravity="left"
        android:lines="1"
        /> 
        <TextView  
        android:id="@+id/caption2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginRight="1dip"   
        android:textSize="15sp"
        android:gravity="left"
        android:lines="1"
        android:paddingLeft="6dip"
        android:layout_weight="2.0"
        /> 
        <TextView  
        android:id="@+id/caption3"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginRight="1dip"   
        android:textSize="11sp"
        android:gravity="left"
        android:lines="1"
        android:layout_weight="1.0"
        /> 
</LinearLayout>


</LinearLayout>     
</LinearLayout> 

and my code to listview

listView = getListView(开发者_如何学Go);
        View v1 = getLayoutInflater().inflate(R.layout.tabhost, null);
        listView.addHeaderView(v1);

        adapter = new SimpleAdapter(this, records_of_module, R.layout.list_main2, dispCols, 
                new int[] { R.id.icon,R.id.caption1,R.id.caption2,R.id.caption3,
                R.id.caption4,R.id.caption5,R.id.caption6 });

        setListAdapter(adapter);


        listView.setOnItemClickListener(this);
        listView.setTextFilterEnabled(true);

and filtering by

adapter.getFilter().filter(s);
adapter.notifyDataSetChanged();

I dont kw from getting exception. My stack trace look like this

FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
at android.widget.AdapterView.checkFocus(AdapterView.java:689)
at android.widget.AdapterView$AdapterDataSetObserver.onInvalidated(AdapterView.java:812)
at android.database.DataSetObservable.notifyInvalidated(DataSetObservable.java:43)
at android.widget.BaseAdapter.notifyDataSetInvalidated(BaseAdapter.java:54)
at android.widget.SimpleAdapter$SimpleFilter.publishResults(SimpleAdapter.java:383)

Please help me by some tips

thanks in advance Joe


It might not solve everything, but the constructor for your SimpleAdapter references fields that are not in your xml file. Your layout contains icon as well as caption1, caption2, and caption3, but not caption4, caption5, or caption6.

According to the documentation for SimpleAdapters the xml resource that you provide must contain at least all of the items that you specify in the last field of the constructor.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号