Only the original thread that created a view hierarchy can touch its
views.  Never update view in  no ui thread!

On 6月5日, 上午6时54分, Hitendrasinh Gohil <hitendra.virtuei...@gmail.com>
wrote:
> Here is my playerview class.when the song first time played total song
> duration is set and seekbar runs,but when i do next the total song
> duration is of firstsong and it gives exception.
>
> package com.musix.main;
>
> import com.musix.main.R;
> import android.app.Activity;
> import android.os.Bundle;
> import android.util.Log;
> import android.view.View;
> import android.view.View.OnClickListener;
> import android.view.Window;
> import android.widget.ImageButton;
> import android.widget.SeekBar;
> import android.widget.TextView;
>
> public class LocalPlayerView extends Activity implements
> OnClickListener {
>         private ImageButton btnnext, btnplay, btnprevious;
>         LocalMediaPlayer mediaplayer;
>         public static boolean drag = false, CHANGE;
>         SeekBar sbCurrentSong;
>         public static int mprogress;
>         private int minutes, seconds, total;
>         private TextView tvSongTotalTime, tvSongCurrentTime, txtsongname,
>                         txtalbumartistname;
>
>         public void onCreate(Bundle instance) {
>                 super.onCreate(instance);
>                 requestWindowFeature(Window.FEATURE_NO_TITLE);
>                 setContentView(R.layout.localplayerview);
>                 mediaplayer = new LocalMediaPlayer(getApplicationContext());
>                 loadUI();
>         }
>
>         private void loadUI() {
>                 try {
>                         btnnext = (ImageButton) findViewById(R.id.next);
>                         btnnext.setOnClickListener(this);
>                         btnplay = (ImageButton) findViewById(R.id.play);
>                         btnplay.setOnClickListener(this);
>                         btnprevious = (ImageButton) 
> findViewById(R.id.previous);
>                         btnprevious.setOnClickListener(this);
>                         txtsongname = (TextView) 
> findViewById(R.id.txtsongname);
>                         txtalbumartistname = (TextView)
> findViewById(R.id.txtalbumartistname);
>                         tvSongTotalTime = (TextView) 
> findViewById(R.id.tvSongTotalTime);
>                         tvSongCurrentTime = (TextView)
> findViewById(R.id.tvSongCurrentTime);
>                         setSongDetails();
>                         startProgress();
>                         sbCurrentSong = (SeekBar) 
> findViewById(R.id.sbCurrentSong);
>                         sbCurrentSong
>                                         .setOnSeekBarChangeListener(new 
> SeekBar.OnSeekBarChangeListener()
> {
>                                                 @Override
>                                                 public void 
> onProgressChanged(SeekBar seekBar,
>                                                                 int progress, 
> boolean fromUser) {
>                                                         String seconds;
>                                                         String str = new 
> String();
>                                                         mprogress = progress;
>                                                         if (fromUser)
>                                                                 
> LocalMediaPlayer.mediaplayer.seekTo(progress);
>
>                                                         
> sbCurrentSong.setProgress(progress);
>
>                                                         seconds = Integer
>                                                                         
> .toString(((progress / 1000) % 60));
>
>                                                         // Here if condition 
> check the second string length
>                                                         // if length
>                                                         // is equal to 1 then 
> we add 0 infront of that
>                                                         // string.
>
>                                                         if (seconds.length() 
> == 1)
>                                                                 str = 
> str.concat("0" + seconds);
>                                                         else
>                                                                 str = seconds;
>                                                         int ctime = 
> ((progress / 1000) / 60);
>
>                                                         // Here if condition 
> check the current time if it
>                                                         // is less than 9 
> then we will add 0 infront of it
>
>                                                         if (ctime > 9) {
>                                                                 
> tvSongCurrentTime.setText(Integer
>                                                                               
>   .toString(ctime) + ":" + str);
>                                                         } else {
>                                                                 
> tvSongCurrentTime.setText("0"
>                                                                               
>   + Integer.toString(ctime) + ":" + str);
>                                                         }
>                                                 }
>
>                                                 @Override
>                                                 public void 
> onStartTrackingTouch(SeekBar seekBar) {
>                                                         // TODO 
> Auto-generated method stub
>                                                 }
>
>                                                 @Override
>                                                 public void 
> onStopTrackingTouch(SeekBar seekBar) { // TODO
>                                                                               
>                                                                           // 
> Auto-generated
>                                                                               
>                                                                           // 
> method
>                                                                               
>                                                                           // 
> stub
>
>                                                 }
>                                         });
>
>                 } catch (Exception e) {
>                         e.printStackTrace();
>                 }
>
>         }
>
>         @Override
>         public void onClick(View v) {
>                 switch (v.getId()) {
>                 case R.id.play:
>                         if (LocalMediaPlayer.mediaplayer.isPlaying()) {
>                                 LocalMediaPlayer.mediaplayer.pause();
>                                 
> btnplay.setBackgroundResource(R.drawable.play_button);
>                                 LocalMediaPlayer.isPaused = true;
>                         } else {
>                                 
> btnplay.setBackgroundResource(R.drawable.pause_button);
>                                 mediaplayer.startPlayer();
>                         }
>
>                         break;
>                 case R.id.next:
>                         
> btnplay.setBackgroundResource(R.drawable.pause_button);
>                         mediaplayer.next();
>                         runOnUiThread(new Runnable() {
>
>                                 @Override
>                                 public void run() {
>                                         setSongDetails();
>                                         //startProgress();
>                                 }
>                         });
>
>                         break;
>                 case R.id.previous:
>                         
> btnplay.setBackgroundResource(R.drawable.pause_button);
>                         mediaplayer.previous();
>                         runOnUiThread(new Runnable() {
>
>                                 @Override
>                                 public void run() {
>                                         setSongDetails();
>                                         //startProgress();
>                                 }
>                         });
>                         break;
>
>                 default:
>                         break;
>                 }
>
>         }
>
>         public void startProgress() {
>                 try {
>                         new Thread() {
>                                 public void run() {
>                                         // mp is your MediaPlayer
>                                         // progress is your ProgressBar
>
>                                         int currentPosition = 0;
>                                         total = 
> LocalMediaPlayer.mediaplayer.getDuration();
>                                         sbCurrentSong.setMax(total);
>                                         setTotalSongDuaration();
>                                         while (LocalMediaPlayer.mediaplayer 
> != null
>                                                         && currentPosition < 
> total) {
>                                                 try {
>                                                         Thread.sleep(1000);
>                                                         currentPosition = 
> LocalMediaPlayer.mediaplayer
>                                                                         
> .getCurrentPosition();
>                                                 } catch (InterruptedException 
> e) {
>                                                         return;
>                                                 } catch (Exception e) {
>                                                         return;
>                                                 }
>                                                 
> sbCurrentSong.setProgress(currentPosition);
>                                         }
>                                 }
>
>                         }.start();
>                 } catch (Exception e) {
>                         e.printStackTrace();
>                 }
>         }
>         public void setSongDetails() {
>                 try {
>                         txtsongname.setText(LocalTrack.LOCALTRACKLIST
>                                         .get(LocalMediaPlayer.CURRENTINDEX));
>                         txtalbumartistname.setText(LocalTrack.LOCALALBUMLIST
>                                         .get(LocalMediaPlayer.CURRENTINDEX)
>                                         + "-"
>                                         + LocalTrack.LOCALARTISTLIST
>                                                         
> .get(LocalMediaPlayer.CURRENTINDEX));
>                 } catch (Exception e) {
>                         e.printStackTrace();
>                 }
>         }
>
>         public void setTotalSongDuaration() {
>                 minutes = (int) ((total / 1000) / 60);
>                 seconds = (int) ((total / 1000) % 60);
>
>                 // Here if condition check minute is less than 10 then it 
> will check
> for
>                 // seconds.
>
>                 if (minutes < 10) {
>                         // Here if condition check if second is less than 9 
> then it will
> add
>                         // 0 infront
>                         // of seconds.
>
>                         if (seconds <= 9)
>                                 tvSongTotalTime.setText("0" + 
> Integer.toString(minutes) + ":"
>                                                 + "0" + 
> Integer.toString(seconds));
>                         else
>                                 tvSongTotalTime.setText("0" + 
> Integer.toString(minutes) + ":"
>                                                 + Integer.toString(seconds));
>                 } else {
>
>                         // Here if condition check if second is less than 9 
> then it will
> add
>                         // 0 infront
>                         // of seconds.
>
>                         if (seconds <= 9)
>                                 
> tvSongTotalTime.setText(Integer.toString(minutes) + ":" + "0"
>                                                 + Integer.toString(seconds));
>                         else
>                                 
> tvSongTotalTime.setText(Integer.toString(minutes) + ":"
>                                                 + Integer.toString(seconds));
>                 }
>
>                 tvSongTotalTime.refreshDrawableState();
>         }
>
> }
>
> Here is the error log.
> W/dalvikvm(  341): threadid=8: thread exiting with uncaught exception
> (group=0x4001d800)
> E/AndroidRuntime(  341): FATAL EXCEPTION: Thread-11
> E/AndroidRuntime(  341): android.view.ViewRoot
> $CalledFromWrongThreadException: Only the original thread that created
> a view hierarchy can touch its views.
> E/AndroidRuntime(  341):        at
> android.view.ViewRoot.checkThread(ViewRoot.java:2802)
> E/AndroidRuntime(  341):        at
> android.view.ViewRoot.requestLayout(ViewRoot.java:594)
> E/AndroidRuntime(  341):        at
> android.view.View.requestLayout(View.java:8125)
> E/AndroidRuntime(  341):        at
> android.view.View.requestLayout(View.java:8125)
> E/AndroidRuntime(  341):        at
> android.view.View.requestLayout(View.java:8125)
> E/AndroidRuntime(  341):        at
> android.view.View.requestLayout(View.java:8125)
> E/AndroidRuntime(  341):        at
> android.view.View.requestLayout(View.java:8125)
> E/AndroidRuntime(  341):        at
> android.widget.TextView.checkForRelayout(TextView.java:5378)
> E/AndroidRuntime(  341):        at
> android.widget.TextView.setText(TextView.java:2688)
> E/AndroidRuntime(  341):        at
> android.widget.TextView.setText(TextView.java:2556)
> E/AndroidRuntime(  341):        at
> android.widget.TextView.setText(TextView.java:2531)
> E/AndroidRuntime(  341):        at
> com.musix.main.LocalPlayerView.setTotalSongDuaration(LocalPlayerView.java:
> 213)
> E/AndroidRuntime(  341):        at com.musix.main.LocalPlayerView
> $4.run(LocalPlayerView.java:162)
>
> can anybody can be how can i solve out this problem?

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to