Android: Base Adapter Weird Null Pointer -
sorry title issue cannot seem summarise
so made class extends autocompletetextview
i made base adapter class implements filterable interface
i set adapter
i null pointer when getcount called states array list null. things weird
this base adapter constructor
public piecesearchadapter(context context, arraylist<food> food) { mcontext = context; mfoods = new arraylist<>(); mfoods.addall(food); }
the above have after diagnosed issue
so impossible array list null now
@override public int getcount() { return mfoods.size(); }
the null pointer still occurs
so debugged @ point in base adapter constructor. items passed in not null , contain items.
i not sure issue is.
stack trace:
java.lang.nullpointerexception @ redacted.foodsearchadapter.getcount(piecesearchadapter.java:36) @ android.widget.autocompletetextview$popupdatasetobserver$1.run(autocompletetextview.java:1291) @ android.os.handler.handlecallback(handler.java:733) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:157) @ android.app.activitythread.main(activitythread.java:5356) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1265) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1081) @ dalvik.system.nativestart.main(native method)
tried karan mer solution
private list<food> mfoods; public piecesearchadapter(context context, arraylist<food> food) { mcontext = context; mfoods = new arraylist<food>(food); }
same problem
checking size of mfoods array in constructor
public piecesearchadapter(context context, arraylist<food> food) { mcontext = context; mfoods = new arraylist<>(food); if(mfoods == null){ debugutils.log("yes null"); } else{ debugutils.log("not null, size " + mfoods.size()); } }
output was
not null, size 11
rohan solution
not try catch same thing
@override public int getcount() { if(mpieces != null){ return mpieces.size(); } return 0; }
does not crash, following exception when changing text of autocompletetextview run filter operation.
344-962/w/filter﹕ exception occured during performfiltering()! java.lang.nullpointerexception @ redacted.foodsearchadapter$foodfilter.performfiltering(foodsearchadapter.java:108) @ android.widget.filter$requesthandler.handlemessage(filter.java:234) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:157) @ android.os.handlerthread.run(handlerthread.java:61)
this line 108. starts
for (food food : mfood) { if (food.getreferencenumber().matches("(?i:.*" + constraint + ".*)") || piece.getname().matches("(?i:.*" + constraint + ".*)")) { filteredfoods.add(food); } }
basically member mfoods null again :(
my entire filter logic
private class foodfilter extends filter { /** * filter data based on pattern in worker thread * * @return filterresults: contains 2 members, count indicating amount of results , data */ @override protected filterresults performfiltering(charsequence constraint) { filterresults results = new filterresults(); //do have constraints passed in? if (!textutils.isempty(constraint)) { arraylist<food> filteredfood = new arraylist<>(); (food food : mfood) { if (food.getreferencenumber().matches("(?i:.*" + constraint + ".*)") || food.getname().matches("(?i:.*" + constraint + ".*)")) { filteredfood.add(food); } } results.count = filteredfood.size(); results.values = filtered; } return results; }
and publishresults method
@override protected void publishresults(charsequence charsequence, filterresults filterresults) { if (filterresults != null) { mfood = (arraylist<food>) filterresults.values; notifydatasetchanged(); } }
more weird. array adapter works me not base adapter
arrayadapter<food> adapter = new arrayadapter<>(getactivity(), android.r.layout.simple_dropdown_item_1line, food);
and in tostring method of food model
@override public string tostring() { return mname; }
so there wrong base adapter im not sure. @ least proves data supplying otherwise not work.
fixed issue.
i had number of problems bojan llievski , rohan pawar pointed out
the key issue here
@override protected void publishresults(charsequence charsequence, filterresults filterresults) { if (filterresults != null) { mfood = (arraylist<food>) filterresults.values; notifydatasetchanged(); } }
so filterresults never null values member can null here setting mfood member null when no results found. , since did not set threshold, no characters instantly make member array list null hense null pointer.
the solution create array list this
private arraylist<food> moriginalfoodpieces; private arraylist<food> mfilteredpiecestoshow = new arraylist<>(); private final context mcontext; public piecesearchadapter(context context, arraylist<food> food) { mcontext = context; moriginalfood = food; }
so other methods getcount , getview use mfilteredpiecestoshow array list. not touch moriginalfood array list need preserve data set filtering.
and publishresults method looks this
@override protected void publishresults(charsequence charsequence, filterresults filterresults) { debugutils.log("publishresults called"); object filterresult = filterresults.values; if(filterresult != null){ mfilteredfoodtoshow = (arraylist<food>) filterresult; } else{ mfilteredfoodtoshow.clear(); } notifydatasetchanged(); }
Comments
Post a Comment