如何使用runOnUiThread更新TextView



我在 Runner 的主要活动中有两个线程,单击按钮即可启动它们。你唯一要做的就是数数。我想更新两个文本视图,其中包含来自 ech 线程的当前计数。当我启动我的应用程序并单击我的按钮时,应用程序崩溃了。

代码在控制台中正常运行。

类运行器仅用于计数。我想在方法运行((中每个段落之后更新两个文本视图。

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线程文档中查看本指南。

无法初始化活动类,只能将其添加到清单中,这样做将导致 null 上下文。因此,删除 UiThread 类中的构造函数。

此外,您不需要运行器类来扩展活动。传递 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();
}
}
}

希望这有帮助。

最新更新