我想让两个 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)