标签:
最近做项目查看bugly上崩溃日志遇到这样的一个问题: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.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()
标签:
原文地址:http://www.cnblogs.com/LvStones/p/5328473.html