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();

加上上面红色两行。

原文地址:https://www.cnblogs.com/antyi/p/3866141.html