android - listview inside a dialog window manager bad token exception -
i working on app clicking image, dialog has open listview inside it. image in listview onclicklistener in getview method of adapter.
when click on image, app stops giving me windowmanagerbadtokenexception on dialog.show() line.
this code :
public class homelistadapter extends arrayadapter<homeitem> { public context context; public arraylist<homeitem> homeitemlist; //arraylist<string> phone; public homelistadapter(context context, int resource, arraylist<homeitem> homeitemlst) { super(context, resource, homeitemlst); this.context = context; this.homeitemlist = homeitemlst; } @override public int getcount() { if (homeitemlist != null) return homeitemlist.size(); return 0; } @override public homeitem getitem(int position) { return homeitemlist.get(position); } @override public view getview(final int position, view convertview, viewgroup parent) { view myconvertview = null; try { final homeitem homeitem = homeitemlist.get(position); myconvertview = convertview; if (myconvertview == null) { layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); myconvertview = inflater.inflate(r.layout.branches_list_item_view, null); } textview catheader = (textview) myconvertview.findviewbyid(r.id.name); textview cattitle = (textview) myconvertview.findviewbyid(r.id.description); imageview imageview = (imageview) myconvertview.findviewbyid(r.id.imageview1); catheader.settext(homeitem.gethomeitemname()); cattitle.settext(homeitem.gethomeitemdescription()); imageview.setonclicklistener(new onclicklistener() { @override public void onclick(view view) { int region = homeitem.gethomeitemid(); alertdialog.builder builder = new alertdialog.builder(context); builder.settitle("phone numbers"); builder.seticon(r.drawable.ic_action_call); listview modelist = new listview(context); arraylist<arraylist<arraylist<string>>> branchesallregions = branchesactivity.branchesallregions; arraylist<string> phone = new arraylist<string>(); for(int = 4 ; < branchesallregions.get(region).get(position).size() ; i++) { phone.add(branchesallregions.get(region).get(position).get(i)); } arrayadapter<string> modeadapter = new arrayadapter<string>(context, android.r.layout.simple_list_item_1, android.r.id.text1, phone); modelist.setadapter(modeadapter); builder.setview(modelist); final dialog dialog = builder.create(); dialog.show(); /*layoutinflater li = (layoutinflater) context.getsystemservice(context.layout_inflater_service); view v = li.inflate(r.layout.dialog_list, null, false); dialog.setcontentview(v); dialog.show();*/ } }); } catch (exception e) { e.printstacktrace(); } return myconvertview; } @override public long getitemid(int position) { return position; } }
this initialized homelistadapter :
private void populatehomepageitemlist(){ adapter = new separatedlistadapter(this); homeitemlist = new arraylist<homeitem>(); for(int = 0 ; < branchesallregions.get(0).size() ; i++){ homeitem homeitem = new homeitem(); homeitem.sethomeitemname(branchesallregions.get(0).get(i).get(0)); homeitem.sethomeitemdescription(branchesallregions.get(0).get(i).get(1)); homeitem.sethomeitemid(0); homeitemlist.add(homeitem); } homelistadapter = new homelistadapter(getapplicationcontext(), 0, homeitemlist); if(homelistadapter!= null) adapter.addsection(regions.get(0), homelistadapter ); homeitemlist = new arraylist<homeitem>(); for(int = 0 ; < branchesallregions.get(1).size() ; i++){ homeitem homeitem = new homeitem(); homeitem.sethomeitemname(branchesallregions.get(1).get(i).get(0)); homeitem.sethomeitemdescription(branchesallregions.get(1).get(i).get(1)); homeitem.sethomeitemid(1); homeitemlist.add(homeitem); } homelistadapter = new homelistadapter(getapplicationcontext(), 0, homeitemlist); if(homelistadapter!= null) adapter.addsection(regions.get(1), homelistadapter ); homeitemlist = new arraylist<homeitem>(); for(int = 0 ; < branchesallregions.get(2).size() ; i++){ homeitem homeitem = new homeitem(); homeitem.sethomeitemname(branchesallregions.get(2).get(i).get(0)); homeitem.sethomeitemdescription(branchesallregions.get(2).get(i).get(1)); homeitem.sethomeitemid(2); homeitemlist.add(homeitem); } homelistadapter = new homelistadapter(getapplicationcontext(), 0, homeitemlist); if(homelistadapter!= null) adapter.addsection(regions.get(2), homelistadapter ); homeitemlist = new arraylist<homeitem>(); for(int = 0 ; < branchesallregions.get(3).size() ; i++){ homeitem homeitem = new homeitem(); homeitem.sethomeitemname(branchesallregions.get(3).get(i).get(0)); homeitem.sethomeitemdescription(branchesallregions.get(3).get(i).get(1)); homeitem.sethomeitemid(3); homeitemlist.add(homeitem); } homelistadapter = new homelistadapter(getapplicationcontext(), 0, homeitemlist); if(homelistadapter!= null) adapter.addsection(regions.get(3), homelistadapter ); homeitemlist = new arraylist<homeitem>(); for(int = 0 ; < branchesallregions.get(4).size() ; i++){ homeitem homeitem = new homeitem(); homeitem.sethomeitemname(branchesallregions.get(4).get(i).get(0)); homeitem.sethomeitemdescription(branchesallregions.get(4).get(i).get(1)); homeitem.sethomeitemid(4); homeitemlist.add(homeitem); } homelistadapter = new homelistadapter(getapplicationcontext(), 0, homeitemlist); if(homelistadapter!= null) adapter.addsection(regions.get(4), homelistadapter ); homeitemlist = new arraylist<homeitem>(); for(int = 0 ; < branchesallregions.get(5).size() ; i++){ homeitem homeitem = new homeitem(); homeitem.sethomeitemname(branchesallregions.get(5).get(i).get(0)); homeitem.sethomeitemdescription(branchesallregions.get(5).get(i).get(1)); homeitem.sethomeitemid(5); homeitemlist.add(homeitem); } homelistadapter = new homelistadapter(getapplicationcontext(), 0, homeitemlist); if(homelistadapter!= null) adapter.addsection(regions.get(6), homelistadapter ); listview.setadapter(adapter); }
this logcat :
08-18 10:43:25.949: e/moreinfohpw_viewgroup(15903): parent view not textview 08-18 10:44:09.344: e/androidruntime(15903): fatal exception: main 08-18 10:44:09.344: e/androidruntime(15903): process: com.example.creditlibanais, pid: 15903 08-18 10:44:09.344: e/androidruntime(15903): android.view.windowmanager$badtokenexception: unable add window -- token null not application 08-18 10:44:09.344: e/androidruntime(15903): @ android.view.viewrootimpl.setview(viewrootimpl.java:731) 08-18 10:44:09.344: e/androidruntime(15903): @ android.view.windowmanagerglobal.addview(windowmanagerglobal.java:278) 08-18 10:44:09.344: e/androidruntime(15903): @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:69) 08-18 10:44:09.344: e/androidruntime(15903): @ android.app.dialog.show(dialog.java:288) 08-18 10:44:09.344: e/androidruntime(15903): @ com.example.creditlibanais.homelistadapter$1.onclick(homelistadapter.java:80) 08-18 10:44:09.344: e/androidruntime(15903): @ android.view.view.performclick(view.java:4626) 08-18 10:44:09.344: e/androidruntime(15903): @ android.view.view$performclick.run(view.java:19300) 08-18 10:44:09.344: e/androidruntime(15903): @ android.os.handler.handlecallback(handler.java:733) 08-18 10:44:09.344: e/androidruntime(15903): @ android.os.handler.dispatchmessage(handler.java:95) 08-18 10:44:09.344: e/androidruntime(15903): @ android.os.looper.loop(looper.java:157) 08-18 10:44:09.344: e/androidruntime(15903): @ android.app.activitythread.main(activitythread.java:5293) 08-18 10:44:09.344: e/androidruntime(15903): @ java.lang.reflect.method.invokenative(native method) 08-18 10:44:09.344: e/androidruntime(15903): @ java.lang.reflect.method.invoke(method.java:515) 08-18 10:44:09.344: e/androidruntime(15903): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1265) 08-18 10:44:09.344: e/androidruntime(15903): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1081) 08-18 10:44:09.344: e/androidruntime(15903): @ dalvik.system.nativestart.main(native method)
i think problem context using show alertdialog , if using getapplicationcontext()
try changing activity context .
edit :
change line :
homelistadapter = new homelistadapter(getapplicationcontext(), 0, homeitemlist)
to :
homelistadapter = new homelistadapter(youractivity.this, 0, homeitemlist)
Comments
Post a Comment