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

来源:互联网 发布:淘宝代销好还是自销好 编辑:程序博客网 时间:2024/06/05 15:51

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


Worker threads are meant for doing background tasks and you can't show anything on UI within a worker thread unless you call method like runOnUiThread. If you try to show anything on UI thread without calling runOnUiThread, there will be a java.lang.RuntimeException.

So, if you are in an activity but calling Toast.makeText() from worker thread, do this:

runOnUiThread(new Runnable() {   public void run()    {      Toast toast = Toast.makeText(getApplicationContext(), "Something", Toast.LENGTH_SHORT).show();       }}); 

The above code ensures that you are showing the Toast message in a UI thread since you are calling it inside runOnUiThread method. So no more java.lang.RuntimeException

0 0