android - error: Only the original thread that created a view hierarchy can touch its views -
hi , thank looking @ question. intermediate programmer in c android newbie. have been trying chat programming working. assuming else in code below works perfectly. 1 question ask when try settext() thread running, exception above. looked @ many many websites , here too. found many things, not understand. please explain me in simple way or offer me simple fix if possible.
thank much!!
public class chatter extends activity { private string name = "unknown user"; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); final edittext msgtoserver = (edittext) findviewbyid(r.id.msgbox); final edittext chatfromserver = (edittext) findviewbyid(r.id.chatbox); final button msgtoserver = (button) findviewbyid(r.id.sendbutton); socket socket = null; string ipaddress = "192.168.1.103"; try { inetaddress serveraddr = inetaddress.getbyname(ipaddress); socket socketmain = new socket(serveraddr, 4444); socket = socketmain; } catch (ioexception e) { // todo auto-generated catch block log.e("tcp", "error", e); } final outmsg outmsg = new outmsg(socket); thread msgsenderthread = new thread(outmsg); msgsenderthread.start(); //chatfromserver.post(new inmsg(socket, chatfromserver)); thread msgreceiverthread = new thread(new inmsg(socket, chatfromserver)); msgreceiverthread.start(); msgtoserver.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string msgtoserverstring; msgtoserverstring = msgtoserver.gettext().tostring(); outmsg.message = name + ": " + msgtoserverstring; outmsg.readytosend = true; msgtoserver.settext(""); } }); } public void updateresultsinui (string msg) { final edittext chatfromserver = (edittext) findviewbyid(r.id.chatbox); chatfromserver.settext(msg); } public class inmsg implements runnable { socket socket; edittext chatfromserver; public inmsg(socket socket, edittext chatfromserver) { this.socket = socket; this.chatfromserver = chatfromserver; } public void run(){ try { bufferedreader in = new bufferedreader(new inputstreamreader(socket.getinputstream())); string str = "firstmessagefromserver"; while (true) { if (str.equals("firstmessagefromserver")) str = in.readline(); else str = str + "\n" + in.readline(); log.e("tcp", "got message: " + str); //here went wrong****************** chatfromserver.settext(str); //****************************************** } } catch (ioexception e) { // todo auto-generated catch block log.e("tcp", "error in receiving", e); } } } @override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.menu, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle item selection switch (item.getitemid()) { case r.id.setnamemenu: setname(); return true; default: return super.onoptionsitemselected(item); } } public void populatechatbox (string msgfroms) { log.e("tcp", "going in popc"); final edittext textnameinput = (edittext) findviewbyid(r.id.namebox); log.e("tcp", " popc"); textnameinput.settext(msgfroms); log.e("tcp", "going out popc"); } public void setname() { setcontentview(r.layout.custom_dialog); final edittext textnameinput = (edittext) findviewbyid(r.id.namebox); button submitnamebutton = (button) findviewbyid(r.id.submitnamebutton); submitnamebutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { string nameinput = textnameinput.gettext().tostring(); if (!name.equals("")) name = nameinput; setcontentview(r.layout.main); } }); } }
in run() method:
message msg = new message(); string texttochange = "text"; msg.obj = texttochange; mhandler.sendmessage(msg);
create mhandler in ui thread;
handler mhandler = new handler() { @override public void handlemessage(message msg) { string text = (string)msg.obj; //call settext here } };
Comments
Post a Comment