Android error系列02:Must be called from main thread of fragment host

问题描述:在Aactivty当中,利用onBackPressed()方法返回到Bactivity(含3个fragement)中的的某一个刚刚进入Aactivity的fragement时出现错误如下



问题原因:在跳转时,必须在主线程中进行,因此可以在handler中利用Looper.getMainLooper()方法


***用Looper.getMainLooper()就表示放到主UI线程去处理。

解决办法:

private Handler handler = new Handler(Looper.getMainLooper());
class MyThread implements Runnable {
public void run() { 
    onBackPressed();//原来想要执行的代码
    }
}
handler.post(new MyThread());//调用方法发送消息(原来想要执行的地方)

猜你喜欢

转载自blog.csdn.net/lucasxu01/article/details/80772090
今日推荐