gpt4 book ai didi

android - 执行两个 Runnables Android

转载 作者:行者123 更新时间:2023-11-29 18:00:15 25 4
gpt4 key购买 nike

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

1) 用于更改 ImageView 的 Runnable 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 的 Runnable 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)

最佳答案

你最好使用线程池:

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

哦,我明白了。问题是您正试图从后台线程而不是主线程更改 TextView 中的文本。为此,您必须使用 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 r1=....(){
bla bla ,你的代码...然后是 handler.sendEmptyMessage(SWITCH_TEXT_CONTENT);}
给你,试试这个会像瑞士 watch 一样工作

关于android - 执行两个 Runnables Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16563378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com