0

我在全局范围内声明了我的 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)

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

4

2 回答 2

6

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

你需要使用这个

runOnUiThread(new Runnable() {
public void run() {
    MyClass.this.txMp3Prog.setText("00:0"+String.valueOf(timer_count));
    }
});
于 2013-07-02T07:38:16.890 回答
4

如日志:

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

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

于 2013-07-02T07:36:37.117 回答