getting java.lang.IllegalStateException in android -


i have action bar drop-down in android application. when user click item drop-down. added alertdialogbuilder user data user, if user press ok save data. when run code got exception. image_log

this code i'm using

@override public boolean onnavigationitemselected(final int i, long l) {       final arraylist<host> hosts = hostfactory.gethosts(this.getapplicationcontext());      // prompts.xml view     layoutinflater li = layoutinflater.from(this);     view promptsview = li.inflate(r.layout.auto_discovery_prompt, null);      alertdialog.builder alertdialogbuilder = new alertdialog.builder(             this);      // set prompts.xml alertdialog builder     alertdialogbuilder.setview(promptsview);      final edittext txtdevicename = (edittext) promptsview.findviewbyid(r.id.txtdevicename_edittextdialoguserinput);     final edittext txtusername = (edittext) promptsview.findviewbyid(r.id.txtusername_edittextdialoguserinput);     final edittext txtpassword = (edittext) promptsview.findviewbyid(r.id.txtpassword_edittextdialoguserinput);     final edittext txtport = (edittext) promptsview.findviewbyid(r.id.txtport_edittextdialoguserinput);     string mac=navspinner.get(i).getmacaddress();      txtdevicename.settext(navspinner.get(i).gettitle());     txtport.settext(navspinner.get(i).getport());     for(spinnernavitem items:navspinner)     {         if(items.gettitle().tostring()!="select device" )         {              // set dialog message             alertdialogbuilder                     .setcancelable(false)                     .setpositivebutton("ok",                             new dialoginterface.onclicklistener() {                                 @targetapi(build.version_codes.gingerbread)                                 public void onclick(dialoginterface dialog,                                                     int id) {                                     // user input , set result                                     // edit text                                     //result.settext(userinput.gettext());                                      string dd=txtpassword.gettext().tostring();                                       host savecurrent_selected_host =new host();                                      savecurrent_selected_host.name=txtdevicename.gettext().tostring();                                     savecurrent_selected_host.addr=navspinner.get(i).getipaddress().tostring();                                      try {                                         savecurrent_selected_host.port = integer.parseint(txtport.gettext().tostring());                                     } catch (numberformatexception e) {                                         savecurrent_selected_host.port = host.default_http_port;                                     }                                      savecurrent_selected_host.user=txtusername.gettext().tostring();                                     savecurrent_selected_host.pass=txtpassword.gettext().tostring();                                      savecurrent_selected_host.esport = host.default_eventserver_port;                                     savecurrent_selected_host.timeout = host.default_timeout;                                     savecurrent_selected_host.access_point="";                                     if(!(navspinner.isempty()&& navspinner.get(i).getmacaddress().tostring().isempty()))                                         savecurrent_selected_host.mac_addr=navspinner.get(i).getmacaddress().tostring();                                     else                                         savecurrent_selected_host.mac_addr="";                                     savecurrent_selected_host.wol_port = host.default_wol_port;                                     savecurrent_selected_host.wol_wait = host.default_wol_wait;                                      hostfactory.addhost(getapplicationcontext(), savecurrent_selected_host);                                     hostfactory.savehost(getapplicationcontext(),savecurrent_selected_host);                                  }                             })                     .setnegativebutton("cancel",                             new dialoginterface.onclicklistener() {                                 public void onclick(dialoginterface dialog,                                                     int id) {                                     dialog.cancel();                                  }                             });              // create alert dialog             alertdialog = alertdialogbuilder.create();              // show             if(!alertdialog.isshowing())                 alertdialog.show();      } } 

how can fix problem?( application braking when come alertdialog.show() )

i believe have 2 issues code, , first issue causing second. first issue displaying new alertdialog each item in navspinner. following if statement

if(items.gettitle().tostring()!="select device" ) 

will true because doing reference comparison (==). want use .equals(). (see here better explanation).

if(!items.gettitle().tostring().equals("select device" )) 

even seems little fishy, want compare loop item selected item, not default string. anyways, result, creating new alertdialog each loop iteration

alertdialog = alertdialogbuilder.create(); 

create() returns new dialog each time, following check

if(!alertdialog.isshowing()) 

is true, new dialog created 1 line above has not been shown yet. means new alertdialog shown

alertdialog.show(); 

on first glance, might expect result in multiple alert dialogs being shown, 1 each item in loop. however, not case, second alert dialog - created during second iteration of loop - cause illegalstateexception. why? because of this:

alertdialogbuilder.setview(promptsview); 

although second dialog own dialog, shares promptsview dialog created during first loop iteration. when show() called, promptsview given parent. ok when first dialog shown, because promptsview new view without parent assigned. however, when second dialog show - second loop iteration - show() method attempts give promptsview parent, time fails because has been given parent first dialog show(). , error: the specified child has parent

in short, fix loop 1 dialog displayed, , should go.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -