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 exception
E/AndroidRuntime( 7200): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

原因是非主线程中默认没有创建Looper对象,需要先调用Looper.prepare()启用Looper。

解决办法:

 

public void sendJson2Server(final JSONObject mJs) {

Handler mHandler = new Handler() {

@Override

public void handleMessage(Message msg) {

// TODO Auto-generated method stub

super.handleMessage(msg);

PushServiceEx.sendPublic(mTopicServer, mJs.toString());

System.out.println(mJs.toString());

}

};

mHandler.sendEmptyMessageDelayed(0, 5000);

// PushServiceEx.sendPublic(mTopicServer, mJs.toString());

}

 

 

new Thread() {

public void run() { 

  Looper.prepare(); 

 mPst.startPushService(); 

  mPst.sendJson2Server(qJson);//上线发消息给server 

 Looper.loop(); 

 }

 }.start();

加上上面红色两行。

posted on 2014-07-24 17:32  antyi  阅读(684)  评论(0编辑  收藏  举报

导航