我用安卓系统制作了一个简单的音乐播放器.每个歌曲的视图都包含一个SeekBar,实现方式如下:

public class Song extends Activity implements OnClickListener,Runnable {
    private SeekBar progress;
    private MediaPlayer mp;

    // ...

    private ServiceConnection onService = new ServiceConnection() {
          public void onServiceConnected(ComponentName className,
            IBinder rawBinder) {
              appService = ((MPService.LocalBinder)rawBinder).getService(); // service that handles the MediaPlayer
              progress.setVisibility(SeekBar.VISIBLE);
              progress.setProgress(0);
              mp = appService.getMP();
              appService.playSong(title);
              progress.setMax(mp.getDuration());
              new Thread(Song.this).start();
          }
          public void onServiceDisconnected(ComponentName classname) {
              appService = null;
          }
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.song);

        // ...

        progress = (SeekBar) findViewById(R.id.progress);

        // ...
    }

    public void run() {
    int pos = 0;
    int total = mp.getDuration();
    while (mp != null && pos<total) {
        try {
            Thread.sleep(1000);
            pos = appService.getSongPosition();
        } catch (InterruptedException e) {
            return;
        } catch (Exception e) {
            return;
        }
        progress.setProgress(pos);
    }
}

这个很好用.现在我想要一个计时器来计算歌曲进度的秒/分钟.所以我在布局中放了一个TextView,在onCreate()中放findViewById(),在progress.setProgress(pos)之后放run():

String time = String.format("%d:%d",
            TimeUnit.MILLISECONDS.toMinutes(pos),
            TimeUnit.MILLISECONDS.toSeconds(pos),
            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(
                    pos))
            );
currentTime.setText(time);  // currentTime = (TextView) findViewById(R.id.current_time);

但最后一行给了我一个例外:

安卓看法ViewRoot$CalledFromErrorThreadException:只有创建视图层次 struct 的原始线程才能接触其视图.

然而,我在这里做的基本上和我在SeekBar上做的一样——在onCreate中创建视图,然后在run()中touch 它——它并没有给我这个抱怨.

推荐答案

您必须将后台任务中更新UI的部分移动到主线程上.这里有一段简单的代码:

runOnUiThread(new Runnable() {

    @Override
    public void run() {

        // Stuff that updates the UI

    }
});

Activity.runOnUiThread美元的文件.

只需将此嵌套在后台运行的方法中,然后复制粘贴在块中间执行任何更新的代码.只包含尽可能少的代码,否则就开始 destruct 后台线程的用途.

Android相关问答推荐

如何使TextField的背景透明?

如何使禁用状态下的material 3按钮与启用状态下的 colored颜色 相同?

无法在Jetpack Compose中显示Admob原生广告

在Jetpack Compose中,material 3 Textfield上的底部边框 colored颜色 是如何更改的?

如何将Unicode符号作为文本居中放置在布局中的按钮(Android XML)中?

在androidStudio中,如何使用带有ResolutionStrategy的ResolutionSelector而不是setTargetResolve()?

Android写/读二进制文件到共享存储

AndroidX Media3 迁移指南

React-Native Manifest 合并失败并出现多个错误

FFmpeg Android 错误

是什么导致调用 Firebase 服务器?

在 react native 中设置 react-native-paper 组件的样式

Jetpack Compose 部分或开放侧边框

AttributionSource 需要 android.permission.BLUETOOTH_SCAN 权限的问题

在移动设备上看到时如何增加 PasswordField 文本?

Jetpack Compose 动画性能问题

Android活动系统导航栏 colored颜色 ?

如何在 TextInputEdit 中调整可绘制对象的大小

ObjectBox,如何在冲突中放弃一切迁移?

即使我在单选按钮上明确设置了选中状态,RecyclerView 中的单选按钮也会随机取消选中