pengwang
pengwang

Reputation: 19966

listView filter mistake

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

and GlycaemicIndexItemAdapter i called from How to write a custom filter for ListView with ArrayAdapter, but when i run this app, it is show:


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)

edit: How to write a custom filter for ListView with ArrayAdapter is very well, if you need you can see this link

Upvotes: 0

Views: 587

Answers (3)

Uday
Uday

Reputation: 6023

Check the line number 33 in testListFilter.java class...!! According to your log.. the field using at that line is getting null..

Upvotes: 1

Sunil Pandey
Sunil Pandey

Reputation: 7102

You forgot to set setContentView(R.layout.yourlayout.xml)

Upvotes: 0

Ganapathy C
Ganapathy C

Reputation: 5999

Add this in first line of onCreate:

items=new ArrayList<GlycaemicIndexItem>();

Upvotes: 1

Related Questions