Execute two Runnables Android

风流意气都作罢 提交于 2019-12-13 17:40:32

问题


I'd like to make two Runnables work for my Android App:

1) Runnable r1 for changing an ImageView:

        Runnable r1 = new Runnable() {

        @Override
        public void run() {
            albumpic.setImageResource(pub[i]);
            i++;
            if(i >= pub.length) {
                i = 0;
            }
            albumpic.postDelayed(this, 3000);
        }
    }; 
    albumpic.postDelayed(r1, 3000);

2) Runnable r2 for changing a 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 I run just one of them it works fine, but when I try to run them both they don't work.

How can I make them work both?

Thanks!

Update: I tried to use the ThreadPoolExecutor like this:

Runnable r1 = new Runnable() {

        @Override
        public void run() {
            albumpic.setImageResource(pub[i]);
            i++;
            if(i >= pub.length) {
                i = 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 it 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: Only the original thread that created a view hierarchy can touch its views.
05-15 11:56:35.695: E/AndroidRuntime(11437):    at android.view.ViewRoot.checkThread(ViewRoot.java:3090)
05-15 11:56:35.695: E/AndroidRuntime(11437):    at android.view.ViewRoot.requestLayout(ViewRoot.java:666)
05-15 11:56:35.695: E/AndroidRuntime(11437):    at android.view.View.requestLayout(View.java:8391)
05-15 11:56:35.695: E/AndroidRuntime(11437):    at android.view.View.requestLayout(View.java:8391)
05-15 11:56:35.695: E/AndroidRuntime(11437):    at android.view.View.requestLayout(View.java:8391)
05-15 11:56:35.695: E/AndroidRuntime(11437):    at android.view.View.requestLayout(View.java:8391)
05-15 11:56:35.695: E/AndroidRuntime(11437):    at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
05-15 11:56:35.695: E/AndroidRuntime(11437):    at android.view.View.requestLayout(View.java:8391)
05-15 11:56:35.695: E/AndroidRuntime(11437):    at android.widget.TextView.checkForRelayout(TextView.java:5880)
05-15 11:56:35.695: E/AndroidRuntime(11437):    at android.widget.TextView.setText(TextView.java:2888)
05-15 11:56:35.695: E/AndroidRuntime(11437):    at android.widget.TextView.setText(TextView.java:2749)
05-15 11:56:35.695: E/AndroidRuntime(11437):    at android.widget.TextView.setText(TextView.java:2718)
05-15 11:56:35.695: E/AndroidRuntime(11437):    at com.adventure.agadir.MainActivity$11.run(MainActivity.java:241)
05-15 11:56:35.695: E/AndroidRuntime(11437):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
05-15 11:56:35.695: E/AndroidRuntime(11437):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
05-15 11:56:35.695: E/AndroidRuntime(11437):    at java.lang.Thread.run(Thread.java:1019)

回答1:


you'd better use a ThreadPool:

ThreadPoolExecutor executor=(ThreadPoolExecutor) Executors.newFixedThreadPool(int n);
//n-is a fixed number of maximum threads to do parallel;
executor.execute(runnable1);
executor.execute(runnable2);

Oh, i see. The problem is that you are trying to change text in a textview from a background thread, not from the main threan. For doing this you got to use Handler- it's kinda like a bridge from the main thread and a background thread http://developer.android.com/reference/android/os/Handler.html

so in your 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 your runnable: Runnable r1=....(){ bla bla , your code... and then handler.sendEmptyMessage(SWITCH_TEXT_CONTENT);} here you go, try this gonna work like a swiss watches



来源:https://stackoverflow.com/questions/16563378/execute-two-runnables-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!