当前位置: 代码迷 >> Android >> android异常之Can't create handler inside thread that has not called Looper.prepare()
  详细解决方案

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

热度:195   发布时间:2016-04-28 06:38:04.0
android错误之Can't create handler inside thread that has not called Looper.prepare()

好久没遇到这种错误,最初都是因为在新开的线程中更新UI才出错,

后来一直没忘记用handler,也就没用错误,

今天有出现如下错误,代码如下:

send.setOnClickListener(new OnClickListener() {						@Override			public void onClick(View v) {				// TODO Auto-generated method stub				new Thread(new Runnable() {										@Override					public void run() {						// TODO Auto-generated method stub		//省略其他代码				Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT).show();					}				});			}		});


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

第一印象就是去Loop.prepare(),后来发现,其实这还是因为在线程中更新UI导致的,

Toast.makeText(getApplicationContext(), "订单发送失败,请重试", Toast.LENGTH_SHORT).show();

把上面这行代码也通过处理放进handler之后,错误就没有了,虽然很简单,但是如果一味去找handler和Loop的问题,会走弯路。希望有所帮助。

 

作者:jason0539

微博:http://weibo.com/2553717707

博客:http://blog.csdn.net/jason0539(转载请说明出处)


 

  相关解决方案