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