2014-01-21 27 views
-1

我試圖實現Runnable並在線程啓動時運行Run()方法。但是當我運行程序時,它墜毀了。實現當線程啓動時的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());(應用崩潰)或僅T1 =新主題();永遠不會崩潰但不輸出。

當線程啓動時,我該如何實現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 
+0

我正在創建這個方法來檢查這個方法是否可以實現run方法。這是我的項目,它有更多的錯誤。我不想先發布這麼多問題。無論如何,我想出瞭如何使線程工作。謝謝。 – Myst

回答

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

0

你不能在一個線程不是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方法中。

相關問題