我是靠谱客的博主 可靠蜗牛,这篇文章主要介绍Android Can‘t toast on a thread that has not called Looper.prepare()错误,现在分享给大家,希望可以做个参考。
已经编写好的专门用于toast显示的函数
//提示信息显示
public void showToast(String msg){
Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
}
通过在另外一个activity调用该方法showToast()
public void onResponse(Call call, Response response) throws IOException {
String result = response.body().string();
showToast(result);
}
出现了Can’t toast on a thread that has not called Looper.prepare(),这是因为主线程才能处理UI操作,在工作线程是不能处理UI操作的
此时我们可以通过以下方式解决该问题
public void onResponse(Call call, Response response) throws IOException {
String result = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
showToast(result);
}
});
}
让我们的Toast方法在主线程执行
最后
以上就是可靠蜗牛最近收集整理的关于Android Can‘t toast on a thread that has not called Looper.prepare()错误的全部内容,更多相关Android内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复