Can't create handler inside thread that has not called Looper.prepare()
来源:互联网 发布:mysql 查询死锁语句 编辑:程序博客网 时间:2024/04/30 05:29
最近做项目查看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() inside AsyncTask
- Handler——Can't create handler inside thread that has not called Looper.prepare()
- 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()错误的解决
- Can't create handler inside thread that has not called Looper.prepare() 异常
- Can't create handler inside thread that has not called Looper.prepare()
- Can’t create handler inside thread that has not called Looper.prepare()
- Can’t create handler inside thread that has not called Looper.prepare()
- Can't create handler inside thread that has not called Looper.prepare()解决办法
- Can't create handler inside thread that has not called Looper.prepare() 终极解决方法
- 异常Can't create handler inside thread that has not called Looper.prepare() 解决方法
- Can't create handler inside thread that has not called Looper.prepare()错误的解决
- Can't create handler inside thread that has not called Looper.prepare() 终极解决方法
- Can't create handler inside thread that has not called Looper.prepare()
- Can't create handler inside thread that has not called Looper.prepare()的终极解决方法
- Can't create handler inside thread that has not called Looper.prepare()错误-安卓编程
- Can’t create handler inside thread that has not called Looper.prepare()
- Can't create handler inside thread that has not called Looper.prepare()解决办法
- MyBatis中Like语句使用方式
- 前端常见面试题(2)
- Windows驱动开发:用C还是C++
- JavaScript 10分钟入门
- shell脚本 逐行读取文本并且 进行字符串的截取
- Can't create handler inside thread that has not called Looper.prepare()
- JAVA中几种集合(List、Set和Map)的区别
- 视差容错
- Android studio错误:IllegalArgumentException: Unable to locate adb
- 编程:等式变换
- 竞品分析和产品分析,你究竟搞清楚了么?
- JavaScript 风格指南/编码规范(Airbnb公司版)
- 搜索框简易处理方式
- Oracle ERP 的前世今生