android - Adapter null pointer when filtering in toolbar searchview -


so i'm trying filter list shown in http://www.tutorialsbuzz.com/2015/11/android-filter-recyclerview-using-searchview-in-toolbar.html

right when click on search icon star typing filter null pointer exception on

adapter.setfilter(filteredmodellist);

activity

public class createmyteamactivity extends appcompatactivity implements searchview.onquerytextlistener {  list<messagedata> messagedatalist = new arraylist<messagedata>();  private recyclerview mrecyclerview; private customrecycleradaptercreateteam adapter;   listview userslistview; context context;  private static final string tag = "myactivity";  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_create_my_team);      context = this;      mrecyclerview = (recyclerview) findviewbyid(r.id.recycleview);      final linearlayoutmanager linearlayoutmanager = new linearlayoutmanager(this);     mrecyclerview.setlayoutmanager(linearlayoutmanager);         messagedata msg1 = new messagedata();     msg1.setname("anant kharod");     msg1.setimage(r.drawable.msgone);      messagedata msg2 = new messagedata();     msg2.setname("adil");     msg2.setimage(r.drawable.msgthree);      messagedatalist.add(msg1);     messagedatalist.add(msg2);      log.i(tag, "message: " + msg1);      //arrmessagedata.add(msg1);      customrecycleradaptercreateteam adapter = new customrecycleradaptercreateteam(this,messagedatalist);     mrecyclerview.setadapter(adapter);     }  @override public boolean oncreateoptionsmenu(menu menu) {     menuinflater inflater = getmenuinflater();     inflater.inflate(r.menu.menu_team, menu);      final menuitem item = menu.finditem(r.id.search);     final searchview searchview = (searchview) menuitemcompat.getactionview(item);     searchview.setonquerytextlistener(this);      menuitemcompat.setonactionexpandlistener(item,             new menuitemcompat.onactionexpandlistener() {                 @override                 public boolean onmenuitemactioncollapse(menuitem item) {                     // when collapsed                        ///here problem                     adapter.setfilter(messagedatalist);                         return true; // return true collapse action view                 }                  @override                 public boolean onmenuitemactionexpand(menuitem item) {                     // when expanded                     return true; // return true expand action view                 }             });     return super.oncreateoptionsmenu(menu);   }  @override public boolean onquerytextsubmit(string query) {     return false; }  @override public boolean onquerytextchange(string newtext) {     final list<messagedata> filteredmodellist = filter(messagedatalist, newtext);     adapter.setfilter(filteredmodellist);     return true; }  private list<messagedata> filter(list<messagedata> models, string query) {     query = query.tolowercase();      final list<messagedata> filteredmodellist = new arraylist<>();     (messagedata model : models) {         final string text = model.getname().tolowercase();         if (text.contains(query)) {             filteredmodellist.add(model);         }     }     return filteredmodellist; } 

adapter:

public class customrecycleradaptercreateteam extends recyclerview.adapter<listviewrowholder>{   private list<messagedata> messagedatalist; private context context;  public customrecycleradaptercreateteam(context context, list<messagedata> messagedatalist){     this.messagedatalist = messagedatalist;     this.context = context; }   @override public listviewrowholder oncreateviewholder(final viewgroup parent, int viewtype) {     view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.create_team_list_item, null);      listviewrowholder holder = new listviewrowholder(v);     return holder; }  @override public void onbindviewholder(listviewrowholder holder, int position) {      messagedata messagedata = messagedatalist.get(position);      holder.person.settext(messagedata.getname());     holder.pic.setimageresource(messagedata.getimage());  }  @override public int getitemcount() {     return messagedatalist.size(); }  public void setfilter(list<messagedata> messagemodels) {     messagedatalist = new arraylist<>();     messagedatalist.addall(messagemodels);     notifydatasetchanged(); } 

}

listviewrowholder

public class listviewrowholder extends recyclerview.viewholder {  public imageview pic; public textview person;  public listviewrowholder(view itemview) {     super(itemview);      this.pic = (imageview) itemview.findviewbyid(r.id.teamimage);     this.person = (textview) itemview.findviewbyid(r.id.teamname); }  public void bind(messagedata messagemodel) {     this.pic.setimageresource(messagemodel.getimage());     this.person.settext(messagemodel.getname()); } 

}

model

public class messagedata {       string message = "";     boolean isread = false;     int images;     string name = "";     string subject = "";     string type = "";      public string getmessage() {         return message;     }      public void setmessage(string message) {         this.message = message;     }      public boolean isread() {         return isread;     }      public void setread(boolean isread) {         this.isread = isread;     }      public void setimage (int images){         this.images = images;     }      public int getimage(){         return images;     }      public string getname(){         return name;     }     public void setname(string name){         this.name = name;     }      public string getsubject(){         return subject;     }     public void setsubject(string subject){         this.subject = subject;     }      public string gettype(){         return type;     }      public void settype(string type){         this.type = type;     }  } 

you have declared global variable

private customrecycleradaptercreateteam adapter; 

but initializing local variable of adapter:

customrecycleradaptercreateteam adapter = new customrecycleradaptercreateteam(this,messagedatalist); 

and when try access global adapter instance, returns null.

change initialization this:

adapter = new customrecycleradaptercreateteam(this,messagedatalist); 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -