【问题标题】:Android "Only the original thread that created a view hierarchy can touch its views."Android“只有创建视图层次结构的原始线程才能触摸它的视图。”
【发布时间】:2011-07-06 22:12:38
【问题描述】:

我在 Android 中构建了一个简单的音乐播放器。每首歌曲的视图包含一个 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);

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

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

然而,我在这里所做的与SeekBar 所做的基本相同——在onCreate 中创建视图,然后在run() 中触摸它——它并没有给我这个抱怨。

【问题讨论】:

    标签: android multithreading


    【解决方案1】:

    RunOnUIThread 似乎对我不起作用,但以下最终解决了我的问题。

                _ = MainThread.InvokeOnMainThreadAsync(async () =>
               {
                   this.LoadApplication(Startup.Init(this.ConfigureServices));
    
                   var authenticationService = App.ServiceProvider.GetService<AuthenticationService>();
                   if (authenticationService.AuthenticationResult == null)
                   {
                       await authenticationService.AuthenticateAsync(AuthenticationUserFlow.SignUpSignIn, CrossCurrentActivity.Current.Activity).ConfigureAwait(false);
                   }
               });
    

    在 Startup.Init 方法中有 ReactiveUI 路由,这需要在主线程上调用。这个 Invoke 方法也比 RunOnUIThread 更好地接受 async/await。

    所以我需要在主线程上调用方法的任何地方都使用它。

    如果有人知道我不知道的事情并可以帮助我改进我的应用程序,请对此发表评论。

    【讨论】:

      猜你喜欢
      • 2016-01-17
      • 1970-01-01
      • 2015-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多