下课睡觉 2016-12-25 15:04 采纳率: 16.7%
浏览 1365
已采纳

关于 Toast 触发android的线程问题

public class ToastUtil {
private static Toast toast = null;
public static void showToast(Context context,String content){
if (toast != null){
toast.cancel();

    }
            Looper.prepare();
    toast = Toast.makeText(context,content,Toast.LENGTH_SHORT);
    toast.show();
    Looper.loop();
}

public static void showToast(Context context,int resid){
    showToast(context,context.getString(resid));
}

}
这是我封装的Toast的代码,其实也就是在网上下载的,我是定义了一个接口,接口里面会用到这个方法,然后接口的方法会在Asynctask中使用,有的时候就会报错toast Can't create handler inside thread that has not called Looper.prepare(),然后在网上搜索,网上的说在 toast = Toast.makeText(context,content,Toast.LENGTH_SHORT);
toast.show();前面加上Looper.prepare();后面加上 Looper.loop();(刚开始我是没有加的),然后就会报错Only one Looper may be created per thread。。。
这是为什么啊?

  • 写回答

4条回答 默认 最新

  • stone_you 2016-12-26 01:00
    关注

    asynctask的实现机制在不同API版本里是有区别的,你使用的是哪个版本的?而且你是在asynctask的哪个回调方法里showtoast的?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog