2013-03-06 75 views
1

只要我執行下面的代碼,媒體播放了60秒,我的應用程序關閉了一個錯誤「不幸的是,YourAPP已停止。」 。如果我刪除「bv.setImageResource(R.drawable.play);」在最後阻止,該應用程序完美的作品。Android上的Try and Catch問題

bv.setImageResource(R.drawable.play)在finally塊上的問題是什麼?

這是我的代碼

bv = (ImageButton) findViewById(R.id.imageButton1); 
private void mpsleep() { 
    mp.start(); 
    Thread timer= new Thread(){ 
     public void run(){ 
      try{ 
       for (i =1;i<60;i++){ 
        sleep(1000);//1 second pause 
       } 
      }catch(Exception e){ 
       e.printStackTrace(); 
      }finally{ 
       mp.pause(); 
       mp.seekTo(0); 
       bv.setImageResource(R.drawable.play); 
      } 
     } 
    }; 
    timer.start(); 
} 

logcat的

03-06 11:03:39.743: V/MediaPlayer(31802): message received msg=4, ext1=0, ext2=0 
03-06 11:03:39.743: V/MediaPlayer(31802): Received seek complete 
03-06 11:03:39.743: V/MediaPlayer(31802): All seeks complete - return to regularly scheduled program 
03-06 11:03:39.743: V/MediaPlayer(31802): callback application 
03-06 11:03:39.743: V/MediaPlayer(31802): back from callback 
03-06 11:03:39.743: E/MediaPlayer(31802): mOnSeekCompleteListener is null. Failed to send MEDIA_SEEK_COMPLETE message. 
03-06 11:03:40.918: V/MediaPlayer(31802): isPlaying: 1 
03-06 11:03:40.918: V/MediaPlayer-JNI(31802): isPlaying: 1 
03-06 11:03:40.918: V/MediaPlayer-JNI(31802): pause 
03-06 11:03:40.918: V/MediaPlayer(31802): pause 
03-06 11:03:40.923: V/MediaPlayer-JNI(31802): seekTo: 0(msec) 
03-06 11:03:40.923: V/MediaPlayer(31802): seekTo 0 
03-06 11:03:40.923: V/MediaPlayer(31802): getDuration 
03-06 11:03:40.923: V/MediaPlayer(31802): message received msg=4, ext1=0, ext2=0 
03-06 11:03:40.923: W/dalvikvm(31802): threadid=11: thread exiting with uncaught exception (group=0x40c541f8) 
03-06 11:03:40.923: V/MediaPlayer(31802): Received seek complete 
03-06 11:03:40.923: V/MediaPlayer(31802): All seeks complete - return to regularly scheduled program 
03-06 11:03:40.923: V/MediaPlayer(31802): callback application 
03-06 11:03:40.923: V/MediaPlayer(31802): back from callback 
03-06 11:03:40.928: E/MediaPlayer(31802): mOnSeekCompleteListener is null. Failed to send MEDIA_SEEK_COMPLETE message. 
03-06 11:03:40.953: E/AndroidRuntime(31802): FATAL EXCEPTION: Thread-20100 
03-06 11:03:40.953: E/AndroidRuntime(31802): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
03-06 11:03:40.953: E/AndroidRuntime(31802): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4286) 
03-06 11:03:40.953: E/AndroidRuntime(31802): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:885) 
03-06 11:03:40.953: E/AndroidRuntime(31802): at android.view.View.requestLayout(View.java:12881) 
03-06 11:03:40.953: E/AndroidRuntime(31802): at android.view.View.requestLayout(View.java:12881) 
03-06 11:03:40.953: E/AndroidRuntime(31802): at android.view.View.requestLayout(View.java:12881) 
03-06 11:03:40.953: E/AndroidRuntime(31802): at android.view.View.requestLayout(View.java:12881) 
03-06 11:03:40.953: E/AndroidRuntime(31802): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:268) 
03-06 11:03:40.953: E/AndroidRuntime(31802): at android.view.View.requestLayout(View.java:12881) 
03-06 11:03:40.953: E/AndroidRuntime(31802): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:268) 
03-06 11:03:40.953: E/AndroidRuntime(31802): at android.view.View.requestLayout(View.java:12881) 
03-06 11:03:40.953: E/AndroidRuntime(31802): at android.widget.ImageView.setImageResource(ImageView.java:316) 
03-06 11:03:40.953: E/AndroidRuntime(31802): at com.hello.YourApp.MainActivity.stopsound(MainActivity.java:118) 
03-06 11:03:40.953: E/AndroidRuntime(31802): at com.hello.YourApp.MainActivity$6.run(MainActivity.java:143) 
03-06 11:03:46.068: D/OpenGLRenderer(31802): Flushing caches (mode 0) 
+1

發佈logcat並讓你初始化bv。 – 2013-03-06 05:49:34

+0

發佈logcat ..我想你可能會面臨空指針異常在bv – 2013-03-06 05:50:37

+0

什麼是bv ??(哪個控件的對象) – 2013-03-06 05:51:30

回答

5

異常消息說,這清楚:

android.view.ViewRootImpl $ CalledFromWrongThreadException:只有創建了 原線視圖層次結構可以觸及其視圖。

您無法從單獨的線程訪問UI元素。刪除

setImageResource(R.drawable.play) 

from run()

如果有一個需要在UI線程上運行一些代碼(Activity類內),您可以使用此方法在活動時間:

runOnUiThread(new Runnable(){ 
    @override 
    public void run(){ 
    //-- put code here-- 
    } 
}); 
+0

謝謝你,我會嘗試。 – 2013-03-06 06:06:25

+0

@SaiKiran你可以在回答結束時用示例代碼包裝'setImageResource(R.drawable.play)'。 – 2013-03-06 06:09:30

1

使用處理器來運行代碼 bv.setImageResource(R .drawable.play);

3

使用UI線程,因爲我在下面這段代碼顯示,

bv = (ImageButton) findViewById(R.id.imageButton1); 
private void mpsleep() { 
    mp.start(); 
    Thread timer= new Thread(){ 
     public void run(){ 
      try{ 
       for (i =1;i<60;i++){ 
        sleep(1000);//5 second pause 
       } 
      }catch(Exception e){ 
       e.printStackTrace(); 
      }finally{ 
       mp.pause(); 
       mp.seekTo(0); 
     runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 
       bv.setImageResource(R.drawable.play); 

     } 
    }); 
      } 
     } 
    }; 
    timer.start(); 
} 

釋:

- >您可能已經編寫bv = (ImageButton) findViewById(R.id.imageButton1);運行在Main thread(UI thread)onCreateonResume方法內。

- >您已經創建了一個新的線程Thread timer= new Thread(),並試圖操縱部件bv。但bv是結合Main Thread(UI thread)。所以你有trouble.to解決這個問題,只需使用runOnUiThread(new Runnable)方法。

我希望這會對您有所幫助!

+0

非常感謝你的工作...... – 2013-03-06 06:13:26

+0

我很樂意幫助你。如果有幫助,請給我回復。我已經編輯了答案,加入了「解釋」來清除你的疑問。 – 2013-03-06 06:16:06