1

我想让两个 Runnables 为我的 Android 应用程序工作:

1) 用于更改 ImageView 的可运行 r1:

        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) 用于更改 TextView 的可运行 r2:

        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);

当我只运行其中一个时,它工作正常,但是当我尝试运行它们时,它们都不起作用。

我怎样才能让它们同时工作?

谢谢!

更新:我尝试像这样使用 ThreadPoolExecutor:

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);

它给了我这些错误:

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)
4

1 回答 1

3

你最好使用线程池:

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

哦,我懂了。问题是您试图从后台线程而不是主线程更改文本视图中的文本。为此,您必须使用 Handler——它有点像主线程和后台线程之间的桥梁 http://developer.android.com/reference/android/os/Handler.html

所以在你的主要课程中:

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(..);

在你的runnable中:Runnable r1=....(){ bla bla , your code... and then handler.sendEmptyMessage(SWITCH_TEXT_CONTENT);} 给你,试试这个会像瑞士手表一样工作

于 2013-05-15T11:33:39.337 回答