listView过滤错误

时间:2011-03-23 11:00:18

标签: android listview

public class testListFilter extends ListActivity {
/** Called when the activity is first created. */
ArrayList<GlycaemicIndexItem> items;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GlycaemicIndexItem gl= new GlycaemicIndexItem();
    gl.setName("wang");
    gl.setimagepath("dfd");
    GlycaemicIndexItem g2= new GlycaemicIndexItem();
    g2.setName("li");
    g2.setimagepath("dfd");
    GlycaemicIndexItem g3= new GlycaemicIndexItem();
    g3.setName("di");
    g3.setimagepath("dfd");
    GlycaemicIndexItem g4= new GlycaemicIndexItem();
    g4.setName("di34");
    g4.setimagepath("dfd");
    GlycaemicIndexItem g5= new GlycaemicIndexItem();
    g5.setName("ai34");
    g5.setimagepath("dfd");
    GlycaemicIndexItem g6= new GlycaemicIndexItem();
    g6.setName("ayu");
    g6.setimagepath("dfd");

    items.add(gl);
    items.add(g2);
    items.add(g3);
    items.add(g4);
    items.add(g5);
    items.add(g6);
    GlycaemicIndexItemAdapter Gi=new GlycaemicIndexItemAdapter(this,1,items);
    getListView().setAdapter(Gi);
   // setListAdapter(Gi);
}
}


   public class GlycaemicIndexItem {
  public String name;
  public String imagepath;
  public String  getName(){
    return name;
    }
      public  void setName( String  name){
   this.name=name;
        }

       public String  getimagepath(){
    return imagepath;
  }
    public  void setimagepath(String  imagepath){
   this.imagepath=imagepath;
    }
 }
我从How to write a custom filter for ListView with ArrayAdapter调用了

和GlycaemicIndexItemAdapter,但是当我运行这个应用程序时,显示:


03-23 10:52:03.903: ERROR/AndroidRuntime(971): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.testListFilter/com.test.testListFilter.testListFilter}: java.lang.NullPointerException


03-23 10:52:03.903: ERROR/AndroidRuntime(971):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)


03-23 10:52:03.903: ERROR/AndroidRuntime(971):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)


03-23 10:52:03.903: ERROR/AndroidRuntime(971):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)


03-23 10:52:03.903: ERROR/AndroidRuntime(971):     at com.test.testListFilter.testListFilter.onCreate(testListFilter.java:33)

编辑:How to write a custom filter for ListView with ArrayAdapter非常好,如果您需要,可以看到此链接

3 个答案:

答案 0 :(得分:1)

检查testListFilter.java类中的第33行 ... !!根据你的日志.. 在该行使用的字段变为空...

答案 1 :(得分:1)

items=new ArrayList<GlycaemicIndexItem>();

在onCreate的第一行添加。

感谢。

答案 2 :(得分:0)

您忘了设置setContentView(R.layout.yourlayout.xml)