Can't create handler inside thread that has not called Looper.prepare()

最近做项目时出现个问题。

在一个基类中,创建一个Handler对象用于主线程向子线程发送数据,代码如下:

this.mThirdHandler = new Handler(){
            @Override
            public void handleMessage(android.os.Message msg) {
                super.handleMessage(msg);
                Bundle bundle = msg.getData();
                isStop = bundle.getBoolean(mContext.getText(R.string.str_message_stop).toString());//isStop为基类中的一个私有成员
            };
        };

但不知道为啥一直报错:Can‘t create handler inside thread that has not called Looper.prepare()。

搜索后发现,原因是此Handler没有Looper。到哪儿去找Looper呢?自己建?

在代码前加入Looper.prepare();,心想这回可以了吧?

没想到依然报错,错误显示,一个主进程只能有一个Looper,要死了。郁闷中...

突然我想到主进程中肯定有Looper,Context.getMainLooper(),再看Handler的实例化时是可以指定Looper的,太爽了,最后代码如下

this.mThirdHandler = new Handler(mContext.getMainLooper()){
            @Override
            public void handleMessage(android.os.Message msg) {
                super.handleMessage(msg);
                Bundle bundle = msg.getData();
                isStop = bundle.getBoolean(mContext.getText(R.string.str_message_stop).toString());
            };
        };

mContext为主界面context,实例化基类时引入的一个参数。

Can't create handler inside thread that has not called Looper.prepare()

时间: 2024-10-25 18:26:30

Can't create handler inside thread that has not called Looper.prepare()的相关文章

Android 线程更新UI报错 : Can't create handler inside thread that has not called Looper.prepare()

MainActivity中有一个按钮,绑定了save方法 public void save(View view) { String title = titleText.getText().toString(); String timelength = lengthText.getText().toString(); ExecutorService exec = Executors.newCachedThreadPool(); exec.execute(new NewsService(getApp

关于子线程使用Toast报错Can't create handler inside thread that has not called Looper.prepare()的解决办法

形同如下代码,在Thread中调用Toast显示错误信息: new Thread(new Runnable(){ @Override public void run() { try{ weatherData = getWeatherData(strUrl); parseJson(weatherData); }catch(Exception e){ Toast.makeText(WindowApplication.getAppContext(), e.toString(), Toast.LENGT

Android : Can't create handler inside thread that has not called Looper.prepare()

又报错了,不过早也习以为常了. Can't create handler inside thread that has not called Looper.prepare() 我把文档给摘录下来了,大家可以看看. 这个类被用于为线程运行消息循环.默认线程并没有消息循环与之关联,所以你需要创建一个,在线程中调用prepare()以运行这个循环,然后调用loop()在循环结束时获取进程信息. 和消息循环交互最多的就是通过Handler类. 下面是一个实现了Looper线程的典型实例,通过分离的pre

使用ClipboardManager碰到Can't create handler inside thread that has not called Looper.prepare()

直接放上我的代码,希望能给碰到同样问题的朋友提供帮助 Runnable runnable = new Runnable() { public void run() { ClipboardManager clipboard = (ClipboardManager)GameUtil.getIntance().getContext().getSystemService(Context.CLIPBOARD_SERVICE); clipboard.setText(content); } }; GameUt

Android handler 报错处理Can't create handler inside thread that has not called Looper.prepare()

解决方法,在ui线程里面创建handler m_MainActivity.runOnUiThread(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub GameBoxUtil.startPay(m_MainActivity,String.valueOf(amount), productName, payCode,orderId,payHandler); }}); Android han

使用ClipboardManager碰到Can't create handler inside thread that has not called Looper.prepare()

直接放上我的代码.希望能给碰到相同问题的朋友提供帮助 Runnable runnable = new Runnable() { public void run() { ClipboardManager clipboard = (ClipboardManager)GameUtil.getIntance().getContext().getSystemService(Context.CLIPBOARD_SERVICE); clipboard.setText(content); } }; GameUt

错误BUG解决:Can't create handler inside thread that has not called Looper.prepare()

我在自定义ContentProvider中,遇到了这个错误: 原因在于: 我在要分享数据的程序中创建数据库的弹出了一个土司,这个土司在主线程中,在第二个程序中,向数据库中添加数据的时候导致其挂了. 切记:不要在主线程中更新UI. 错误BUG解决:Can't create handler inside thread that has not called Looper.prepare()

Android java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

E/AndroidRuntime(7200): Uncaught handler: thread Thread-8 exiting due to uncaught exceptionE/AndroidRuntime( 7200): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 原因是非主线程中默认没有创建Looper对象,需要先调用Looper

解决使用Handler时Can't create handler inside thread that has not called Looper.prepare()

在android开发中,主线程不能进行耗时操作,所以我们经常把耗时操作放在子线程中进行,那么就需要子线程与主线程相互交流,就需要使用到handler. 而在使用handler过程中,如果对handler使用不太熟练的话就偶尔会出现Can't create handler inside thread that has not called Looper.prepare()的报错异常.之前在Handler的原理的博文中有讲到,Handler的使用会依靠Looper循环来发送消息,如果不创建Loope