我在Runner的主要活动中有两个线程,然后单击一个按钮启动它们。你唯一要做的就是数数。我想更新两个TextViews与ech线程的当前计数。当我启动我的应用程序并单击我的按钮时,该应用程序崩溃了。

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

类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上查阅本指南。

您无法初始化活动类,只能将其添加到清单中,否则将导致空上下文。因此,请在您的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();
    }
}

}


希望这可以帮助。

07-24 09:49