TextView:只有创建视图层次结构的原始线程才能触及其视图

时间:2013-07-02 07:33:08

标签: android

我全局声明了我的TextView和timer_count:

TextView txMp3Prog;
int timer_count=0;

在onCreate()中我做了

txMp3Prog = (TextView)findViewById(R.id.txMp3Prog);

我正在尝试获取歌曲的持续时间并将文本设置如下

  txMp3Prog.setText("00:0"+String.valueOf(timer_count));

文本设置在这里

@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); 


            }

但是当我播放一首歌时,我会收到以下错误记录:

   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)

如何在run方法中将文本正确设置为textview。

2 个答案:

答案 0 :(得分:5)

除主UI线程外,没有其他线程可以访问或更改UI的行为。

你需要使用这个

runOnUiThread(new Runnable() {
public void run() {
    MyClass.this.txMp3Prog.setText("00:0"+String.valueOf(timer_count));
    }
});

答案 1 :(得分:4)

在日志中:

  

CalledFromWrongThreadException:仅创建的原始线程   视图层次结构可以触及其视图。

表示您正在尝试从非UI线程更新或访问UI元素,因此请使用HandlerrunOnUiThread更新非UI线程中的视图