关于子线程使用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.LENGTH_SHORT).show();
                    Log.i("wytings",e.toString());
                }
            }
        }).start();

一运行,就会报错Can‘t create handler inside thread that has not called Looper.prepare(),因为Toast的初始化函数中,自己开了个线程new Handler();所以使得当前的Toast要是不在主线程就会报错。

解决办法如下,在Toast上下添加Looper.prepare();和Looper.loop();

Looper.prepare();
Toast.makeText(WindowApplication.getAppContext(), e.toString(), Toast.LENGTH_SHORT).show();
Looper.loop();

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

时间: 2024-10-10 12:48:42

关于子线程使用Toast报错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

转 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()

在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException:  Can't create handler inside thread that has not called Looper.prepare()  这是因为Handler对象与其调用者在同一线程中,如果在Handler中设置了延时操作,则调用线程也会堵塞.每个Handler对象都会绑定一个Looper对象,每个Looper对象对应一个消息队列(MessageQueue).如果在创建Ha

Android蓝牙开发,报BluetoothAdapter﹕ Can't create handler inside thread that has not called Looper.prepare

这个错误翻译的意思是:不能在没有Looper.prepare的线程里面创建handler. 起初我很疑惑,我根本没有用到工作线程,也没有创建handler.报错的代码如下: // Device scan callback. private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final Blu

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

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

我是在用okhttp的请求数据,在处理数据的时候,打开了一个dialog用来提示,然后报了这个错误. 经过调试,发现错误原因是: dialog必须在一个被Looper.prepare()回调的的线程里创建,但是okhttp这个线程不具备这个条件 <span style="font-size:14px;">OkHttpUtil.getDataInGet(updateUrl, new Callback() { @Override public void onFailure(Re

windows下编译caffe报错:error MSB4062: 未能从程序集 E:\NugetPackages\OpenCV.2.4.10\......的解决办法

参考博客:http://blog.csdn.net/u013277656/article/details/75040459 在windows上编译caffe时,用vs打开后会自动加载还原NugetPackages文件夹,里面是caffe需要依赖的各种库. 费了很长时间还原完成编译时,出现错误:error MSB4062: 未能从程序集 E:\NugetPackages\OpenCV.2.4.10\build\native\private\coapp.NuGetNativeMSBuildTasks

在子线程中更改主线程中的控件的信息,在子线程中用toast

一丶在子线程中不允许更改主线程中的控件的信息,也不允许在子线程中用toast,我们要更改的话 (1)消息机制:使用handler (由主线程调用) 在主程序中Handler handler = new Handler(){ public void handleMessage(Message msg){ int type = msg.what ;//拿到msg的类型,再判断            switch (type) {                case SUCCESS:      

&lt;&lt;&lt; tomcat启动是报错StandardServer.await: create[8005]

启动tomcat的时候出现异常 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 严重: StandardServer.await: create[8005]: java.net.BindException: Address already in use: JVM_Bind         at java.net.PlainSocketImpl.socketBind(Native Method) at java.net.PlainSocketImpl.bind(Plai

MyEclipse打开 HTML 报错Failed to create the part&#39;s controls

拷贝代码时有时会弹出这个错误,页面仍然可以访问,但是无法编辑很郁闷. MyEclipse默认打开编辑页面是MyEclipse visual html designer 右击html页面选择open with,点击 MyEclipse html Editer即可. MyEclipse打开 HTML 报错Failed to create the part's controls