gpt4 book ai didi

java - 如何使用 runOnUiThread 更新 TextView

转载 作者:行者123 更新时间:2023-12-01 16:22:21 24 4
gpt4 key购买 nike

我的主要 Activity 中有两个来自 Runner 的线程,并通过单击按钮启动它们。你唯一要做的就是数数。我想用 ech 线程的当前计数更新两个 TextView。当我启动应用程序并单击按钮时,应用程序崩溃了。

代码在控制台中完美运行。

Runner类仅用于计数。我想在方法 running() 中的每个段落之后更新两个 TextView。

public class Runner extends Activity implements Runnable {

int count = 0;
String name;



public Runner(String name) {
super();
this.name = name;
}

public void warten() throws InterruptedException {
int zahl = (int) (Math.random() * 500) + 500;
Thread.sleep(zahl);
}

public void running() throws InterruptedException {
for(int i = 0; i < 10; i++) {
warten();
count++;
System.out.println(name + " ist bei: " + count);

if(count == 10) {
System.out.println(name + " IST FERTIG");
}

runOnUiThread(new UiThread(name, count));

}


}

public void run() {
try {
running();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

UiThread 类应该是更新 UI 的主线程。

public class UiThread extends Activity implements Runnable {

String name;
int count;

TextView textView1;
TextView textView2;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textView1 = findViewById(R.id.textView2);
textView2 = findViewById(R.id.textView3);

}


public UiThread(String name, int count){
this.name = name;
this.count = count;
}


@Override
public void run() {
if(name == "thread1"){
textView1.setText("thread1 ist bei: " + count);
}else{
textView2.setText("thread2 ist bei: " + count);
}
}
}

最佳答案

我建议您查看本关于 Android 线程的指南 docs .

您无法初始化 Activity 类,只能将其添加到 list 中,这样做会导致上下文为空。因此,请删除 UiThread 类中的构造函数。

此外,您不需要您的运行者类来扩展 Activity。传递 Activity 的实例以便使用 runOnUiThread() 方法。

下面是 UiTHread 类应该是什么样子的示例。

public class UiThread extends Activity {

public TextView textView1;
public TextView textView2;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

textView1 = findViewById(R.id.textView2);
textView2 = findViewById(R.id.textView3);

startThreads();
}

private void startThreads(){
// Initialize the runnable class passing this activity context.
Runner runner = new Runner(this);

Thread thread1 = new Thread(runner, "thread1");
Thread thread2 = new Thread(runner, "thread2");

thread1.start();
thread2.start();
}
}

运行者类别

public class Runner implements Runnable {


private int count = 0;
private UiThread context;

public Runner(UiThread activityContext) {
this.context = activityContext;
}

public void warten() throws InterruptedException {
int zahl = (int) (Math.random() * 500) + 500;
Thread.sleep(zahl);
}



public void running() throws InterruptedException {
for(int i = 0; i < 10; i++) {
warten();
count++;
System.out.println(Thread.currentThread().getName() + " ist bei: " + count);

if(count == 10) {
System.out.println(Thread.currentThread().getName() + " IST FERTIG");
}

// update text views from the main thread.
context.runOnUiThread(new Runnable() {
@Override
public void run() {
if(Thread.currentThread().getName().equals("thread1")){
context.textView1.setText("thread1 ist bei: " + count);
}else{
context.textView2.setText("thread2 ist bei: " + count);
}
}
});
}


}

public void run() {
try {
running();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

希望这有帮助。

关于java - 如何使用 runOnUiThread 更新 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62234856/

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