Can't create handler inside thread that has not called Looper.prepare()
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Can't create handler inside thread that has not called Looper.prepare()相关的知识,希望对你有一定的参考价值。
最近做项目查看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 productName, final int productCount, final 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()的主要内容,如果未能解决你的问题,请参考以下文章
在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()(示例代码
转 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()(示例
Can't create handler inside thread that has not called Looper.prepare()
Android开发:Can't create handler inside thread that has not线程问题解决
Android handler 报错处理Can't create handler inside thread that has not called Looper.prepare()(示例代码
Can't create handler inside thread that has not called Looper.prepare()