Question

As soon i execute the below code, the media is played for 60seconds and my app closes with an error "Unfortunately, YourAPP has stopped." . If i remove "bv.setImageResource(R.drawable.play);" on finally block, the app works perfectly.

what is the problem with bv.setImageResource(R.drawable.play) on finally block ??

This is my code

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)
Was it helpful?

Solution

Exception message says it clearly:

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

You cannot access UI elements from separate thread. Remove

setImageResource(R.drawable.play) 

from run().

If there's a little code (inside Activity class) that needs to be run on UI thread, you can use this method in Activity:

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

OTHER TIPS

Use UI thread as I have shown in following piece of code,

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

Explaination:

-> You might have written bv = (ImageButton) findViewById(R.id.imageButton1); inside your onCreate or onResume method which runs on Main thread(UI thread).

-> You have created a new thread Thread timer= new Thread() and trying to manipulate the component bv.but bv is bind to Main Thread(UI thread).so you are having trouble.to resolve it,simply use runOnUiThread(new Runnable) method.

I hope it will be helpful !

Use handler to run your code bv.setImageResource(R.drawable.play);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top