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

1. 程序时,出现运行时异常,如上图所示。

2.异常原因分析。在ContentObserver的onChange方法中,调用了Toast.makeText方法。onChange方法应该在子线程运行,在android中的子线程中不能直接控制UI组件,否则就会报异常

3.Looper类别用来为一个线程开启一个消息循环。默认情况下Android中新诞生的线程是没有开启消息循环的。(主线程除外,主线程系统会自动为其创建Looper对象,开启消息循环)

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

时间: 2025-01-17 05:29:54

java.lang.RuntimeException: 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

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

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

10-12 17:02:55.500: E/AndroidRuntime(28343): FATAL EXCEPTION: Timer-2 10-12 17:02:55.500: E/AndroidRuntime(28343): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 10-12 17:02:55.500: E/AndroidRuntim

转 在子线程中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

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