I declared my TextView and timer_count globally:
TextView txMp3Prog;
int timer_count=0;
in onCreate() i did
txMp3Prog = (TextView)findViewById(R.id.txMp3Prog);
I am trying to get duration of song and set the text as follows
txMp3Prog.setText("00:0"+String.valueOf(timer_count));
The text is set here
@Override
public void run() {
// TODO Auto-generated method stub
int currentPosition= 0;
int total = mp.getDuration();
while (mp!=null && currentPosition<total) {
try {
Thread.sleep(1000);
currentPosition= mp.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
sbMusicProgress.setProgress(currentPosition);
timer_count++;
System.out.println( "Current position: "+timer_count);
txMp3Prog.setText("00:0"+String.valueOf(timer_count));
String currenttext = String.valueOf(currentPosition);
String songDuration = currenttext.substring(0, currenttext.length() / 2);
}
But when i play a song i get the error log below:
07-02 11:25:36.575: E/AndroidRuntime(9418): FATAL EXCEPTION: Thread-13294
07-02 11:25:36.575: E/AndroidRuntime(9418): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-02 11:25:36.575: E/AndroidRuntime(9418): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4867)
07-02 11:25:36.575: E/AndroidRuntime(9418): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:979)
07-02 11:25:36.575: E/AndroidRuntime(9418): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4306)
07-02 11:25:36.575: E/AndroidRuntime(9418): at android.view.View.invalidate(View.java:10519)
07-02 11:25:36.575: E/AndroidRuntime(9418): at android.view.View.invalidate(View.java:10474)
07-02 11:25:36.575: E/AndroidRuntime(9418): at android.widget.TextView.checkForRelayout(TextView.java:6603)
07-02 11:25:36.575: E/AndroidRuntime(9418): at android.widget.TextView.setText(TextView.java:3710)
07-02 11:25:36.575: E/AndroidRuntime(9418): at android.widget.TextView.setText(TextView.java:3568)
07-02 11:25:36.575: E/AndroidRuntime(9418): at android.widget.TextView.setText(TextView.java:3543)
07-02 11:25:36.575: E/AndroidRuntime(9418): at package.ResultatMultiple.run(ResultatMultiple.java:290)
07-02 11:25:36.575: E/AndroidRuntime(9418): at java.lang.Thread.run(Thread.java:856)
How can i set the text to the textview properly in run method.