zoukankan      html  css  js  c++  java
  • 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();

    加上上面红色两行。

  • 相关阅读:
    Log4Net_LayOut
    各种数据库的批量插入操作_Oracle
    关于各种数据库 Insert时同时取到Id的操作
    一个小型的DBHelper的诞生(1)
    设计模式:重读(抽象工厂模型)
    设计模式:重读(单例)
    设计模式:重读(导读)
    17 1
    5
    oj 2.2
  • 原文地址:https://www.cnblogs.com/antyi/p/3866141.html
Copyright © 2011-2022 走看看