Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android中从单独的线程设置文本_Java_Android_Multithreading - Fatal编程技术网

Java 如何在Android中从单独的线程设置文本

Java 如何在Android中从单独的线程设置文本,java,android,multithreading,Java,Android,Multithreading,我想从线程中设置文本 这是我的线程代码: private class GenerateThread implements Runnable { public void run(){ // generate the first music music = generate(prevmusic, prevmusic.length); prevmusic = music; // write the

我想从线程中设置文本

这是我的线程代码:

private class GenerateThread implements Runnable {
    public void run(){
            // generate the first music
            music = generate(prevmusic, prevmusic.length);
            prevmusic = music;

            // write the midi
            writeMidi(music, song);
            textOut.setText("Initialising...");
            });
    }
}
在我的主代码中,我使用

Thread t = new Thread(new GenerateThread());
    t.start();
它不允许我从线程中设置text。 在互联网上的一些帖子之后,我尝试使用一个处理程序,但这给了我错误,我认为我是双重定义的可运行方式

处理器处理器(主处理器之前)


如何从线程中设置文本?谢谢

只能从UI线程更新UI。将允许您在下次执行UI线程时在其上运行操作。您可以执行以下操作:

runOnUiThread(new Runnable() {
  public void run() {
      textOut.setText("Initialising...");
  }
});
编辑:

private class GenerateThread implements Runnable {
   public void run(){
      // generate the first music
      music = generate(prevmusic, prevmusic.length);
      prevmusic = music;

      // write the midi
      writeMidi(music, song);

      // Update the UI
      MyActivity.this.runOnUiThread(new Runnable() {
        public void run() {
          textOut.setText("Initialising...");
        }
      });
   }
}

除了
runOnUiThread
之外,这里还有一个我更喜欢的方法,因为您不需要外部活动的丑陋引用(
MyActivity.this.runOnUiThread()

也不要混淆
Runnable
Thread
Runnable
只是一个带有
run()
方法的普通类。它可以并且经常在新的
线程上执行。如果需要,您还可以将
GenerateThread
制作成真正的
线程,如下所示:

private class GenerateThread extends Thread {
    @Override
    public void run() {
        // code here.
    }
}
// start somewhere
new GenerateThread().start();

除了使用经典的
Thread
之外,您还可以考虑使用
AsyncTask
,因为这正是为那些长时间运行并且需要在以后或有进展时更新UI的任务而做的。

OK。如果我把这个代码放在我的主程序中。我如何开始线程?我不能使用线程t=new Thread(new GenerateThread());t.开始();因为它不是一个单独的类了?
private class GenerateRunnable implements Runnable {
    public void run() {
        // this code is executed in a background thread.
        // generate the first music
        music = generate(prevmusic, prevmusic.length);
        prevmusic = music;

        // write the midi
        writeMidi(music, song);
        textOut.post(new Runnable() {
            public void run() {
                // this code is executed on the UI thread.
                textOut.setText("Initialising...");
            }
        });
    }
}

@Override
protected void onResume() {
    super.onResume();
    new Thread(new GenerateRunnable()).start();
}
private class GenerateThread extends Thread {
    @Override
    public void run() {
        // code here.
    }
}
// start somewhere
new GenerateThread().start();