我无法重现此问题,但我收到了一些错误报告
异常 android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图。
样品
SampleActivity extends Activity{
private Messenger callBackMessenger = new Messenger(new CallBackHandler(this));
}
private static class CallBackHandler extends Handler {
public CallBackHandler(SampleActivity activity) {
context = new WeakReference<>(activity);
}
@Override
public void handleMessage(Message msg) {
TestActivity a = context.get();
if(a!=null){
a.textView.setText("text"); <<crash
}
}
来自onStart和onStop的绑定和unBind调用
handleMessage应在UI中处理(如果在UI中创建)。我想纠正错误行为的原因。 runOnUiThread是解决方法。
答案 0 :(得分:0)
将a.uodateUI放入runOnUiThread
public CallBackHandler(TestActivity activity){ context = new WeakReference&lt;&gt;(activity); }
@Override
public void handleMessage(Message msg) {
TestActivity a = context.get();
if(a!=null){
a.runOnUiThread(new Runnable () {
public void run() {
a.updateUI(); <<crash
}
}) ;
}
}