1

执行以下代码后,媒体播放 60 秒,我的应用程序关闭并显示错误“不幸的是,YourAPP 已停止。” . 如果我删除“bv.setImageResource(R.drawable.play);” 在 finally 块上,该应用程序运行良好。

finally 块上的 bv.setImageResource(R.drawable.play) 有什么问题?

这是我的代码

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();
}

日志猫

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)
4

3 回答 3

5

异常消息清楚地说明了这一点:

android.view.ViewRootImpl$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能接触其视图。

您不能从单独的线程访问 UI 元素。消除

setImageResource(R.drawable.play) 

run().

如果有一些代码(在 Activity 类中)需要在 UI 线程上运行,您可以在 Activity 中使用此方法:

runOnUiThread(new Runnable(){
  @override
  public void run(){
    //-- put code here--
  }
});
于 2013-03-06T05:56:37.173 回答
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);在您的onCreateoronResume方法中编写了在Main thread(UI thread).

-> 你已经创建了一个新线程Thread timer= new Thread()并试图操作组件。bv但是bv绑定到了Main Thread(UI thread)。所以你遇到了麻烦。解决它,只需使用runOnUiThread(new Runnable)方法。

我希望它会有所帮助!

于 2013-03-06T06:05:03.043 回答
1

使用处理程序运行您的代码 bv.setImageResource(R.drawable.play);

于 2013-03-06T05:57:19.370 回答