android - Custom List Adapter Not Working -
i customizing android listview. following code of custtom adapter
public class myadapter extends baseadapter { context context; arraylist<menu_itemss>list; string[] textarray; int[] images; menu_itemss m; public class menu_itemss { public string text; public int image; public menu_itemss(string text,int images) { this.text=text; this.image=images; } } public class viewholders { imageview imageview; textview textview; public viewholders(view view) { textview=(textview)view.findviewbyid(r.id.textview4); imageview=(imageview)view.findviewbyid(r.id.imageview); } } public myadapter(context contexts,string[] arr) { list=new arraylist<menu_itemss>(); textarray=arr; this.context=contexts; images= new int[]{r.drawable.logo, r.drawable.logo, r.drawable.logo, r.drawable.logo}; (int i=0;i<textarray.length;i++) { m=new menu_itemss(textarray[i],images[i]); list.add(m); } } public int getcount() { return list.size(); } @override public object getitem(int position) { return list.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { view row=convertview; viewholders holder = null; if (row==null) { layoutinflater inflater=(layoutinflater) context.getsystemservice(context.layout_inflater_service); inflater.inflate(r.layout.list,parent,false); holder=new viewholders(row); row.settag(holder); } else { holder=(viewholders)row.gettag(); } menu_itemss item=list.get(position); holder.imageview.setimageresource(item.image); holder.textview.settext(item.text); return row; } }
and here declaration of object of listview using adapter
listview listview=(listview)findviewbyid(r.id.list); listadapter adapter=new myadapter(getapplicationcontext(),s); listview.setadapter(adapter);
when build code , android studio not show error when run on android device following error displayed
unfortunately ! application stopped working!
log cat :
fatal exception: main process: com.example.mashood.muftiattaullahmultanicom, pid: 27743 java.lang.nullpointerexception: attempt invoke virtual method 'android.view.view android.view.view.findviewbyid(int)' on null object reference @ com.example.mashood.muftiattaullahmultanicom.myadapter$viewholders.<init>(myadapter.java:48) @ com.example.mashood.muftiattaullahmultanicom.myadapter.getview(myadapter.java:95) @ android.widget.abslistview.obtainview(abslistview.java:2402) @ android.widget.listview.makeandaddview(listview.java:1891) @ android.widget.listview.filldown(listview.java:713) @ android.widget.listview.fillfromtop(listview.java:779) @ android.widget.listview.layoutchildren(listview.java:1699) @ android.widget.abslistview.onlayout(abslistview.java:2206) @ android.view.view.layout(view.java:16075) @ android.view.viewgroup.layout(viewgroup.java:5300) @ android.widget.relativelayout.onlayout(relativelayout.java:1077) @ android.view.view.layout(view.java:16075) @ android.view.viewgroup.layout(viewgroup.java:5300) @ android.widget.framelayout.layoutchildren(framelayout.java:579) @ android.widget.framelayout.onlayout(framelayout.java:514) @ android.view.view.layout(view.java:16075) @ android.view.viewgroup.layout(viewgroup.java:5300) @ android.widget.linearlayout.setchildframe(linearlayout.java:1703) @ android.widget.linearlayout.layoutvertical(linearlayout.java:1557) @ android.widget.linearlayout.onlayout(linearlayout.java:1466) @ android.view.view.layout(view.java:16075) @ android.view.viewgroup.layout(viewgroup.java:5300) @ android.widget.framelayout.layoutchildren(framelayout.java:579) @ android.widget.framelayout.onlayout(framelayout.java:514) @ android.view.view.layout(view.java:16075) @ android.view.viewgroup.layout(viewgroup.java:5300) @ android.widget.linearlayout.setchildframe(linearlayout.java:1703) @ android.widget.linearlayout.layoutvertical(linearlayout.java:1557) @ android.widget.linearlayout.onlayout(linearlayout.java:1466) @ android.view.view.layout(view.java:16075) @ android.view.viewgroup.layout(viewgroup.java:5300) @ android.widget.framelayout.layoutchildren(framelayout.java:579) @ android.widget.framelayout.onlayout(framelayout.java:514) @ android.view.view.layout(view.java:16075) @ android.view.viewgroup.layout(viewgroup.java:5300) @ android.view.viewrootimpl.performlayout(viewrootimpl.java:2119) @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1873) @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1073) @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:5988) @ android.view.choreographer$callbackrecord.run(choreographer.java:767) @ android.view.choreographer.docallbacks(choreographer.java:580) @ android.view.choreographer.doframe(choreographer.java:550) @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:753) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5910) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1405) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1200)
any in regard appreciated.thank you
please assign inflated view row.
row = inflater.inflate(r.layout.list,parent,false);
its crashing because passing null object viewholders.
Comments
Post a Comment