码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-03-28 13:20:25      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:

最近做项目查看bugly上崩溃日志遇到这样的一个问题:Can‘t create handler inside thread that has not called Looper.prepare()

java.lang.RuntimeException

Can‘t create handler inside thread that has not called Looper.prepare()

android.os.Handler.<init>(Handler.java:200)
android.os.Handler.<init>(Handler.java:114)
android.widget.Toast$TN.<init>(Toast.java:345)
android.widget.Toast.<init>(Toast.java:100)
android.widget.Toast.makeText(Toast.java:256)
com.bsgamesdk.android.uo.utils.ToastUtil.showToast(ToastUtil.java:8)
org.cocos2dx.cpp.CommonSDK.gameSdkPay(CommonSDK.java:279)
org.cocos2dx.cpp.CommonSDK.pay(CommonSDK.java:265)
org.cocos2dx.lib.Cocos2dxRenderer.nativeRender(Native Method)
org.cocos2dx.lib.Cocos2dxRenderer.onDrawFrame(Cocos2dxRenderer.java:110)
android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1523)

android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)

 

我翻阅我对接充值sdk的代码,有这么一段

public void gameSdkPay(final int moneyAmount,final String productNamefinal int productCountfinal String tradeNo,final String subject, final String extInfo)

{

  if(!m_GameSdkProxy.isLogin((Activity)m_Context)){

           ToastUtil.showToast((Activity)m_Context, "你还没登录");

     Log.d(Shixc_TAG,"gameSdkPay===你还没登录");

            return;

        }

  ……

}

这个gameSdkPay接口里有调用ToastUtil.showToast的地方,看log是这个接口出问题了。

public static void showToast(String content) 

{

  Message msg = new Message();

  msg.what = 1;

  msg.obj = content;

  if (m_CommonSDK.mHandler != null)

  {

  m_CommonSDK.mHandler.sendMessage(msg);

  }

}

那就是说mHandler初始化出错了

private Handler mHandler = null;

private void initHandler() 

{

  mHandler = new Handler(

  {

    @Override

    public void handleMessage(Message msg) 

    {

      super.handleMessage(msg);

      switch (msg.what) 

      {

        case 1:

        Toast.makeText(m_Context, (String) msg.obj,

        Toast.LENGTH_SHORT).show();

        break;

      }

    }

  };

}

但不知道为啥一直报错:Can‘t create handler inside thread that has not called Looper.prepare()。

思索很久,感觉原因是此Handler没有Looper。到哪儿去找Looper呢?自己建?

突然我想到主进程中肯定有Looper,m_Context.getMainLooper(),再看Handler的实例化时是可以指定Looper的,最后代码如下

private void initHandler() 

{

  mHandler = new Handler(m_Context.getMainLooper()) 

  {

    @Override

    public void handleMessage(Message msg) 

    {

      super.handleMessage(msg);

      switch (msg.what) 

      {

        case 1:

        Toast.makeText(m_Context, (String) msg.obj,

        Toast.LENGTH_SHORT).show();

        break;

      }

    }

  };

}

 

修复如上后,好了!

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

标签:

原文地址:http://www.cnblogs.com/LvStones/p/5328473.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!