在线程启动时实现Runnable Run()

时间:2014-01-21 18:06:51

标签: java android

我正在尝试实现Runnable并在线程启动时运行Run()方法。但是当我运行该程序时,它崩溃了。

MainActivity

public class MainActivity extends Activity implements Runnable{

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Thread t1;
            t1=new Thread(this);
            t1.start();
        }

        public void run() {
            // TODO Auto-generated method stub
            Toast.makeText(MainActivity.this, "display something",
                    Toast.LENGTH_LONG).show();
        }

我尝试将其更改为t1=new Thread(new MainActivity());(app崩溃)或只是t1 =新的Thread();从不崩溃但没有输出。

如何在线程启动时实现Runnable Run?我搜遍了整个地方但找不到答案。我需要在我的主项目代码中包含此功能。但我创建了一个单独的测试项目,只是为了了解它是如何工作的,所以我可以自己将它添加到我的主项目代码中。在我的主要项目中,它此时也崩溃了。它从未到达Run方法。

崩溃后,这是 LogCat

01-21 13:03:06.460: W/dalvikvm(879): threadid=11: thread exiting with uncaught exception (group=0xb3a6fb90)
01-21 13:03:06.460: E/AndroidRuntime(879): FATAL EXCEPTION: Thread-51
01-21 13:03:06.460: E/AndroidRuntime(879): Process: com.example.testthreadrun, PID: 879
01-21 13:03:06.460: E/AndroidRuntime(879): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
01-21 13:03:06.460: E/AndroidRuntime(879):  at android.os.Handler.<init>(Handler.java:200)
01-21 13:03:06.460: E/AndroidRuntime(879):  at android.os.Handler.<init>(Handler.java:114)
01-21 13:03:06.460: E/AndroidRuntime(879):  at android.widget.Toast$TN.<init>(Toast.java:327)
01-21 13:03:06.460: E/AndroidRuntime(879):  at android.widget.Toast.<init>(Toast.java:92)
01-21 13:03:06.460: E/AndroidRuntime(879):  at android.widget.Toast.makeText(Toast.java:241)
01-21 13:03:06.460: E/AndroidRuntime(879):  at com.example.testthreadrun.MainActivity.run(MainActivity.java:29)
01-21 13:03:06.460: E/AndroidRuntime(879):  at java.lang.Thread.run(Thread.java:841)
01-21 13:03:07.010: I/Choreographer(879): Skipped 126 frames!  The application may be doing too much work on its main thread.
01-21 13:03:07.970: I/Choreographer(879): Skipped 165 frames!  The application may be doing too much work on its main thread.
01-21 13:03:08.840: D/gralloc_goldfish(879): Emulator without GPU emulation detected.
01-21 13:03:10.770: I/Choreographer(879): Skipped 31 frames!  The application may be doing too much work on its main thread.
01-21 13:03:26.670: I/Process(879): Sending signal. PID: 879 SIG: 9

3 个答案:

答案 0 :(得分:3)

由于您要更新UI,因此您需要在UI Thread上进行更新。您应该使用runOnUiThread()AsyncTask

之类的内容
 runOnUiThread(new Runnable()
{
    @Override
    public void run()
    {
            Toast.makeText(MainActivity.this, "display something",
                Toast.LENGTH_LONG).show();
    }
 });

Example of AsyncTask

AsyncTask Docs

答案 1 :(得分:0)

您无法从后台线程更新ui。您只能从ui线程更新ui。

您可以使用runOnUiThread。但是为了只显示一个祝酒词你为什么需要一个帖子?。

http://developer.android.com/guide/components/processes-and-threads.html

答案 2 :(得分:0)

您不能在不是UI线程的线程上进行UI更改(如吐司)。请改用:

public void run() {
        MainActivity.this.runOnUiThread(new Runnable(){

              @Override
              public void run(){
                    Toast.makeText(MainActivity.this, "display something",
                Toast.LENGTH_LONG).show();
              }
        });

 }

但是,为什么要创建一个完整的单独线程来显示吐司?您最好只在Toast.makeText(MainActivity.this, "display something", Toast.LENGTH_LONG).show();方法中添加onCreate