java - set adapter null pointer -
i have been trying solve problem few hours now, failed figure out.i try set adapter expandable list view take error. thanks. "attempt invoke virtual method 'void android.widget.expandablelistview.setadapter(android.widget.expandablelistadapter)' on null object reference"
main_activity
public class mainactivity extends appcompatactivity implements view.onclicklistener { button b1; private residemenu residemenu; private context mcontext; private residemenuitem itemanasayfa; private residemenuitem itemrastgele; private residemenuitem itemencok; private residemenuitem itemtarifyaz; private expandablelistview listview; private expandablelistadapter listadapter; private list<string> listdataheader; private hashmap<string,list<string>> listhash; @override protected void oncreate(bundle savedinstancestate) { listview=(expandablelistview)findviewbyid(r.id.exp2); initdata(); listadapter=new com.yeni.expandablelistadapter(this,listdataheader,listhash); listview.setadapter(listadapter); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mcontext = this; setupmenu(); if (savedinstancestate == null) { changefragment(new f_ana_sayfa()); } } private void initdata() { listdataheader=new arraylist<>(); listhash=new hashmap<>(); listdataheader.add("et"); listdataheader.add("tavuk"); listdataheader.add("sebze"); listdataheader.add("diger"); list<string> l_et=new arraylist<>(); l_et.add("kemikli"); l_et.add("kuşbaşı"); list<string> l_tavuk=new arraylist<>(); l_tavuk.add("bonfile"); l_tavuk.add("but"); l_tavuk.add("bütün tavuk"); list<string> l_sebze=new arraylist<>(); l_sebze.add("patates"); l_sebze.add("biber"); l_sebze.add("kabak"); l_sebze.add("patlıcan"); list<string> l_diger=new arraylist<>(); l_diger.add("tuz"); l_diger.add("yağ"); l_diger.add("kekik"); listhash.put(listdataheader.get(0),l_et); listhash.put(listdataheader.get(1),l_tavuk); listhash.put(listdataheader.get(2),l_sebze); listhash.put(listdataheader.get(3),l_diger); } private void setupmenu() { residemenu = new residemenu(this); residemenu.setbackground(r.drawable.menu_background); residemenu.attachtoactivity(this); residemenu.setmenulistener(menulistener); residemenu.setscalevalue(0.6f); //create menu items; itemanasayfa = new residemenuitem(this, r.drawable.icon_home, "ana sayfa"); itemrastgele = new residemenuitem(this, r.drawable.icon_profile, "random tarif bul"); itemencok = new residemenuitem(this, r.drawable.icon_calendar, "en cok begenilenler"); itemtarifyaz = new residemenuitem(this, r.drawable.icon_settings, "tarif yaz"); itemanasayfa.setonclicklistener(this); itemrastgele.setonclicklistener(this); itemencok.setonclicklistener(this); itemtarifyaz.setonclicklistener(this); residemenu.addmenuitem(itemanasayfa, residemenu.direction_left); residemenu.addmenuitem(itemrastgele, residemenu.direction_left); residemenu.addmenuitem(itemencok, residemenu.direction_right); residemenu.addmenuitem(itemtarifyaz, residemenu.direction_right); findviewbyid(r.id.title_bar_left_menu).setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { residemenu.openmenu(residemenu.direction_left); } }); findviewbyid(r.id.title_bar_right_menu).setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { residemenu.openmenu(residemenu.direction_right); } }); } @override public boolean dispatchtouchevent(motionevent ev) { return residemenu.dispatchtouchevent(ev); } @override public void onclick(view view) { if (view == itemanasayfa) { changefragment(new f_ana_sayfa()); } else if (view == itemrastgele) { changefragment(new f_rastgele_tarif()); } else if (view == itemencok) { changefragment(new f_encok_beg()); } else if (view == itemtarifyaz) { changefragment(new f_tarif_yaz()); } else changefragment(new f_ana_sayfa()); residemenu.closemenu(); } private residemenu.onmenulistener menulistener = new residemenu.onmenulistener() { @override public void openmenu() { toast.maketext(mcontext, "menu opened!", toast.length_short).show(); } @override public void closemenu() { toast.maketext(mcontext, "menu closed!", toast.length_short).show(); } }; private void changefragment(fragment targetfragment) { residemenu.clearignoredviewlist(); getsupportfragmentmanager() .begintransaction() .replace(r.id.main_fragment, targetfragment, "fragment") .settransitionstyle(fragmenttransaction.transit_fragment_fade) .commit(); } public residemenu getresidemenu(){ return residemenu;} }
adapter class
public class expandablelistadapter extends baseexpandablelistadapter { private context cont; private list<string> listdataheader; private hashmap<string,list<string>> listhashmap; public expandablelistadapter(context cont, list<string> listdataheader, hashmap<string, list<string>> listhashmap) { this.cont = cont; this.listdataheader = listdataheader; this.listhashmap = listhashmap; } @override public int getgroupcount() { return listdataheader.size(); } @override public int getchildrencount(int i) { return listhashmap.get(listdataheader.get(i)).size(); } @override public object getgroup(int i) { return listdataheader.get(i); } @override public object getchild(int i, int i1) { // i=group position i=item position return listhashmap.get(listdataheader.get(i)).get(i1); } @override public long getgroupid(int i) { return i; } @override public long getchildid(int i, int i1) { return i1; } @override public boolean hasstableids() { return false; } @override public view getgroupview(int i, boolean b, view view, viewgroup viewgroup) { string headertitle=(string)getgroup(i); if (view==null){ layoutinflater inflater1=(layoutinflater)this.cont.getsystemservice(context.layout_inflater_service); view=inflater1.inflate(r.layout.list_group,null); } textview lblheader=(textview)view.findviewbyid(r.id.header); lblheader.settypeface(null, typeface.bold); lblheader.settext(headertitle); return view; } @override public view getchildview(int i, int i1, boolean b, view view, viewgroup viewgroup) { final string childtext=(string)getchild(i,i1); if (view==null){ layoutinflater inflater1=(layoutinflater)this.cont.getsystemservice(context.layout_inflater_service); view=inflater1.inflate(r.layout.list_item,null); } textview txtlistchild=(textview)view.findviewbyid(r.id.list_item); txtlistchild.settext(childtext); return view; } @override public boolean ischildselectable(int i, int i1) { return true; } }
activity_main xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@android:color/white" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <expandablelistview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/exp2" /> <linearlayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/layout_top"> <!--<imageview--> <!--android:layout_width="match_parent"--> <!--android:layout_height="3dp"--> <!--android:background="#2ea3fe"/>--> <framelayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <button android:layout_width="28dp" android:layout_height="28dp" android:background="@drawable/titlebar_menu_selector" android:id="@+id/title_bar_left_menu" android:layout_gravity="left|center_vertical" android:layout_marginleft="10dp"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="7dp" android:text="yemek do" android:textsize="24sp" android:layout_gravity="center" android:textcolor="?attr/actionmodesplitbackground" /> <button android:layout_width="28dp" android:layout_height="28dp" android:background="@drawable/titlebar_menu_selector" android:id="@+id/title_bar_right_menu" android:layout_gravity="right|center_vertical" android:layout_marginright="10dp"/> </framelayout> <view android:layout_width="match_parent" android:layout_height="5dp" android:background="#bcb8b8"/> </linearlayout> <framelayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/main_fragment"> <relativelayout android:layout_width="match_parent" android:layout_height="match_parent"> <button android:text="button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_marginleft="107dp" android:layout_marginstart="107dp" android:layout_margintop="188dp" android:id="@+id/button2" /> <button android:text="button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_alignright="@+id/button2" android:layout_alignend="@+id/button2" android:layout_margintop="78dp" android:id="@+id/button3" /> </relativelayout> </framelayout> </linearlayout>
what doing:
listview=(expandablelistview)findviewbyid(r.id.exp2); initdata(); listadapter=new com.yeni.expandablelistadapter(this,listdataheader,listhash); listview.setadapter(listadapter); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main);
you intialize listview object before setcontentview wrong. should this
super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); listview=(expandablelistview)findviewbyid(r.id.exp2); initdata(); listadapter=new com.yeni.expandablelistadapter(this,listdataheader,listhash); listview.setadapter(listadapter);
Comments
Post a Comment