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

关于 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条)

报告相同问题?