multithreading - Execute two Runnables Android -
i'd make 2 runnables work android app:
1) runnable r1 changing imageview:
runnable r1 = new runnable() { @override public void run() { albumpic.setimageresource(pub[i]); i++; if(i >= pub.length) { = 0; } albumpic.postdelayed(this, 3000); } }; albumpic.postdelayed(r1, 3000);
2) runnable r2 changing textview:
runnable r2 = new runnable(){ @override public void run(){ out = "title: " + retriever.extractmetadata(shoutcastmetadataretriever.metadata_key_title) + " \nartist: " + retriever.extractmetadata(shoutcastmetadataretriever.metadata_key_artist); title.settext(out); title.postdelayed(this, 3000); } }; title.postdelayed(r2, 3000);
when run 1 of them works fine, when try run them both don't work.
how can make them work both?
thanks!
update: tried use threadpoolexecutor this:
runnable r1 = new runnable() { @override public void run() { albumpic.setimageresource(pub[i]); i++; if(i >= pub.length) { = 0; } albumpic.postdelayed(this, 3000); } }; runnable r2 = new runnable() { @override public void run() { out = "title: " + retriever.extractmetadata(shoutcastmetadataretriever.metadata_key_title) + " \nartist: " + retriever.extractmetadata(shoutcastmetadataretriever.metadata_key_artist); title.settext(out); title.postdelayed(this, 3000); } }; threadpoolexecutor executor = (threadpoolexecutor) executors.newfixedthreadpool(2); executor.execute(r1); executor.execute(r2);
and gave me these errors:
05-15 11:56:35.695: e/androidruntime(11437): fatal exception: pool-2-thread-2 05-15 11:56:35.695: e/androidruntime(11437): android.view.viewroot$calledfromwrongthreadexception: original thread created view hierarchy can touch views. 05-15 11:56:35.695: e/androidruntime(11437): @ android.view.viewroot.checkthread(viewroot.java:3090) 05-15 11:56:35.695: e/androidruntime(11437): @ android.view.viewroot.requestlayout(viewroot.java:666) 05-15 11:56:35.695: e/androidruntime(11437): @ android.view.view.requestlayout(view.java:8391) 05-15 11:56:35.695: e/androidruntime(11437): @ android.view.view.requestlayout(view.java:8391) 05-15 11:56:35.695: e/androidruntime(11437): @ android.view.view.requestlayout(view.java:8391) 05-15 11:56:35.695: e/androidruntime(11437): @ android.view.view.requestlayout(view.java:8391) 05-15 11:56:35.695: e/androidruntime(11437): @ android.widget.relativelayout.requestlayout(relativelayout.java:257) 05-15 11:56:35.695: e/androidruntime(11437): @ android.view.view.requestlayout(view.java:8391) 05-15 11:56:35.695: e/androidruntime(11437): @ android.widget.textview.checkforrelayout(textview.java:5880) 05-15 11:56:35.695: e/androidruntime(11437): @ android.widget.textview.settext(textview.java:2888) 05-15 11:56:35.695: e/androidruntime(11437): @ android.widget.textview.settext(textview.java:2749) 05-15 11:56:35.695: e/androidruntime(11437): @ android.widget.textview.settext(textview.java:2718) 05-15 11:56:35.695: e/androidruntime(11437): @ com.adventure.agadir.mainactivity$11.run(mainactivity.java:241) 05-15 11:56:35.695: e/androidruntime(11437): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1088) 05-15 11:56:35.695: e/androidruntime(11437): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:581) 05-15 11:56:35.695: e/androidruntime(11437): @ java.lang.thread.run(thread.java:1019)
you'd better use threadpool:
threadpoolexecutor executor=(threadpoolexecutor) executors.newfixedthreadpool(int n); //n-is fixed number of maximum threads parallel; executor.execute(runnable1); executor.execute(runnable2);
oh, see. problem trying change text in textview background thread, not main threan. doing got use handler- it's kinda bridge main thread , background thread http://developer.android.com/reference/android/os/handler.html
so in main class :
private final const int switch_text_contet=1; private handler handler; protected void oncreate(...){ handler=new handler(){ @override public void handlemessage(message msg) { if(msg==switch_text_content) textview.settext(..);
and in runnable: runnable r1=....(){ bla bla , code... , handler.sendemptymessage(switch_text_content);}
here go, try gonna work swiss watches
Comments
Post a Comment