Android: Alert dialog throws exception inside socket thread -


i having following android code, where-in android app contacts socket program , result "success" in thread model. after that, trying show alert dialog, android program gets exception thread exiting uncaught exception couldn't what's wrong here. can't show alert inside thread? please advise.

public class randomidactivity extends activity {          .............           clientthread = new clientthread();              button connectbtn = (button) findviewbyid(r.id.button2);             connectbtn.setonclicklistener(new view.onclicklistener() {                  @override                 public void onclick(view v) {                     // todo auto-generated method stub                      thread t = new thread(new clientthread());                     t.start();                 }             });          }   class clientthread implements runnable {         @override         public void run()         {             // got ip servlet , stored temporarily, retrieve there.             string socketserverip = ((globalstore) randomidactivity.this.getapplication()).getsocketipaddress();             log.d("socketserverip", socketserverip);              try {                 inetaddress serveraddr = inetaddress.getbyname(socketserverip);                 socket = new socket(serveraddr, serverport);                  pw = new printwriter(socket.getoutputstream(), true);                 edittext randtxtfield = (edittext) findviewbyid(r.id.edittext1);                 pw.println(randtxtfield.gettext().tostring());                 //output.write(imgbyte,0,imgbyte.length);                 pw.flush();                               // read randrom id returned socket                 bufferedreader socketreader;                 try {                     socketreader = new bufferedreader(new inputstreamreader(socket.getinputstream()));                     msgstr = socketreader.readline();                     log.d("msgstr: ", msgstr);                      if (msgstr.equalsignorecase("success") )                     {                         socket.close();                   // crashing if call alert dialog this.                         cobrowsealertdialog("successfully connected! click ok starts screen sharing!", true);                          //intent intent = new intent(context, mainactivity.class);                         //startactivity(intent);                     }                     else                     {                         cobrowsealertdialog("there seems problem in connecting..try connecting again proper random auth id!", false);                     }                  } catch (ioexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 }              }             catch (unknownhostexception e1) {                 e1.printstacktrace();             }             catch (ioexception e1) {                 e1.printstacktrace();             }           }     }       public void cobrowsealertdialog(string msg, boolean bmove) {              alertdialog.builder builder = new alertdialog.builder(randomidactivity.this); // getparent()             builder.settitle("cobrowsing")             .setmessage(msg)             .setcancelable(false)             .setnegativebutton("ok",new dialoginterface.onclicklistener() {                 public void onclick(dialoginterface dialog, int id) {                     dialog.cancel();                      //intent intent = new intent(context, mainactivity.class);                     //startactivity(intent);                 }             });             alertdialog alert = builder.create();             alert.show();         }      } 

logcat:

03-24 17:12:20.353: d/socketserverip(32717): 192.168.1.21 03-24 17:12:20.503: d/msgstr:(32717): success 03-24 17:12:29.952: w/dalvikvm(32717): threadid=17: thread exiting uncaught exception (group=0x415efba8) 03-24 17:12:33.926: w/jdwp(32717): debugger telling vm exit code=1 

in android , thread not directly update ui thread. if u want update ur ui , u should use

handler.

 handler mhandler;       @override     public void run()     {         // got ip servlet , stored temporarily, retrieve there.         string socketserverip = ((globalstore) randomidactivity.this.getapplication()).getsocketipaddress();         log.d("socketserverip", socketserverip);          try {             inetaddress serveraddr = inetaddress.getbyname(socketserverip);             socket = new socket(serveraddr, serverport);              pw = new printwriter(socket.getoutputstream(), true);             edittext randtxtfield = (edittext) findviewbyid(r.id.edittext1);             pw.println(randtxtfield.gettext().tostring());             //output.write(imgbyte,0,imgbyte.length);             pw.flush();                           // read randrom id returned socket             bufferedreader socketreader;             try {                 socketreader = new bufferedreader(new inputstreamreader(socket.getinputstream()));                 msgstr = socketreader.readline();                 log.d("msgstr: ", msgstr);    //add handler here    mhandler.post(new runnable{ public void run(){    if (msgstr.equalsignorecase("success") )                 {                     socket.close();               // crashing if call alert dialog this.                     cobrowsealertdialog("successfully connected! click ok starts screen sharing!", true);                      //intent intent = new intent(context, mainactivity.class);                     //startactivity(intent);                 }                 else                 {                     cobrowsealertdialog("there seems problem in connecting..try connecting again proper random auth id!", false);                 }         }    });                  } catch (ioexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }          }         catch (unknownhostexception e1) {             e1.printstacktrace();         }         catch (ioexception e1) {             e1.printstacktrace();         }       } } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -