java - 如何使用 runOnUiThread 更新 TextView

标签 java android android-studio android-runonuithread

我的主要 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/

相关文章:

android - java.lang.ArithmeticException : divide by zero in onActivityResult 异常

java - 如何滚动我的屏幕

android - 如何在android studio中增加撤销和重做步骤

android - 当我在 user-interface-samples 项目中运行 Notifications 应用程序时,是什么原因导致 "java.lang.NullPointerException"错误?

java - 如何使用 jsoup 删除标签但保留给定的标签

java - 在 Java 8 中,用于连接的 '+' 运算符被 new StringBuilder() 取代

android - "No Connected Devices",试图将我的 LG 连接到我的 Ubuntu 机器

java - 如何在没有 JEditor 的情况下制作 Java Web 浏览器?

java - 处理 "Usage: PApplet [options] <class name> [sketch args]"

java - 无法解析 R.array.items 中的 "array"(android studio)