0

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.

Dimitri
  • 677
  • 3
  • 19
  • 46
  • 2
    possible duplicate of [Android "Only the original thread that created a view hierarchy can touch its views."](http://stackoverflow.com/questions/5161951/android-only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-vi) – OcuS Jul 02 '13 at 07:35
  • Did you even search online ? – OcuS Jul 02 '13 at 07:36
  • 2
    You can not touch UI widgets in other thread and runnable. So need to call runOnUiThread(). – Rushabh Patel Jul 02 '13 at 07:37

2 Answers2

6

No other thread except the main UI thread can access or change the behavior of the UI.

You need to use this

runOnUiThread(new Runnable() {
public void run() {
    MyClass.this.txMp3Prog.setText("00:0"+String.valueOf(timer_count));
    }
});
PravinCG
  • 7,688
  • 3
  • 30
  • 55
4

as in log:

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

means you are trying to update or access UI element from non ui Thread so use Handler or runOnUiThread for updating Views from non UI Thread

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213