multithreading - App crashes, when a thread is executed, Android? -
i trying update textview in time interval. ie texts changes time changes.the contents stored in array, when try thread , app crashes . here source code:
import android.content.intent; import android.graphics.typeface; import android.net.uri; import android.support.v4.app.dialogfragment; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.window; import android.widget.imageview; import android.widget.textview; import java.util.calendar; public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final messagelist sample = new messagelist(); sample.messagemethod(); final calendar c = calendar.getinstance(); new thread(new runnable() { @override public void run() { int x = 0; while (i<1) { if(c.get(calendar.minute)%2==0) { ++x; textview alertmessage = (textview)findviewbyid(r.id.messages); string parse = sample.message[x]; alertmessage.settext(parse); } } } }).start();
the app working before using thread, don't know if it's due error in initializing. suggestions?
you have run part of code in ui thread
:
textview alertmessage = (textview)findviewbyid(r.id.messages); string parse = sample.message[x]; alertmessage.settext(parse);
it should like:
new thread(new runnable(){ @override public void run(){ int x=0; while(i<1){ if(c.get(calendar.minute)%2==0){ ++x; runonuithread(new runnable(){ @override public void run(){ textview alertmessage=(textview)findviewbyid(r.id.messages); string parse=sample.message[x]; alertmessage.settext(parse); } }); } } } }).start();
Comments
Post a Comment