Android蓝牙开发,报BluetoothAdapter﹕ Can't create handler inside thread that has not called Looper.prepare
来源:互联网 发布:尚硅谷linux视频教程 编辑:程序博客网 时间:2024/06/17 05:27
这个错误翻译的意思是:不能在没有Looper.prepare的线程里面创建handler。
起初我很疑惑,我根本没有用到工作线程,也没有创建handler。报错的代码如下:
// Device scan callback. private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { final iBeaconClass.iBeacon ibeacon = iBeaconClass.fromScanData(device, rssi, scanRecord); if (mFilter.isDevice(ibeacon)) { scanLeDevice(false);//停止扫描 T.showShort(getActivity(), getString(R.string.sign_in_success)); AppSettings.setPrefString(getActivity(), Config.ISSIGNIN, mCurrentDate); setButtonState();//改变按钮的状态 DialogUtils.proGone(); // mControl.closeBlue();//关闭蓝牙 } } };
百度了下,网上的做法是如下:
Looper.prepare();BluetoothAdapter.getDefaultAdapter();
在获取BluetoothAdapter的之前,加上Looper.prepare();,然并卵。问了很多人,都说不知,这时候这能自力更生了。最后终于被我找到解决办法。
思路:我查看了SDK的源码,发现原来我再调用Toast的时候,创建了handler,源码如下,
当我查看TN这个类的时候,就发现了问题的所在
类 TN 是一个aidl的.Stub的子类,而Toast的显示和隐藏都是通过此类进行通信的,而里面使用handler,具体原理我就不深究下去了。我于是猜想 new BluetoothAdapter.LeScanCallback()的onLeScan可能并没有运行在主线程,于是我调用了runOnUiThread()方法,结果果然解决了。修改后代码如下:
// Device scan callback. private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { final iBeaconClass.iBeacon ibeacon = iBeaconClass.fromScanData(device, rssi, scanRecord); if (mFilter.isDevice(ibeacon)) { scanLeDevice(false);//停止扫描 getActivity().runOnUiThread(new Runnable() {//UI线程的控件改变状态,需要调用此方法,不然可能会无效 @Override public void run() { T.showShort(getActivity(), getString(R.string.sign_in_success)); AppSettings.setPrefString(getActivity(), Config.ISSIGNIN, mCurrentDate); setButtonState(); DialogUtils.proGone(); // mControl.closeBlue();//关闭蓝牙 } }); } } };
到此,问题已解决,为了让其他同道碰到此问题,而减少寻找bug的时间,特写此博客,告知!
0 0
- Android蓝牙开发,报BluetoothAdapter﹕ Can't create handler inside thread that has not called Looper.prepare
- Android开发报错 Can't create handler inside thread that has not called Looper.prepare()
- Android开发报Can't create handler inside thread that has not called Looper.prepare()错误
- Android开发报错 Can't create handler inside thread that has not called Looper.prepare()
- Android 报Can't create handler inside thread that has not called Looper.prepare()错误!
- Android开发Can't create handler inside thread that has not called Looper.prepare()
- 使用okhttp 报Can't create handler inside thread that has not called Looper.prepare()
- Unity-Android: Can't create handler inside thread that has not called Looper.prepare()
- Android 错误提示: Can't create handler inside thread that has not called Looper.prepare()
- android之Can't create handler inside thread that has not called Looper.prepare()
- android can't create handler inside thread that has not called Looper.prepare()解决方案
- [Android]Can't create handler inside thread that has not called Looper.prepare()
- Android中的Can't create handler inside thread that has not called Looper.prepare()异常
- Android进阶(八)Can't create handler inside thread that has not called Looper.prepare()
- Android : Can't create handler inside thread that has not called Looper.prepare() 异常
- android -- Can't create handler inside thread that has not called Looper.prepare()
- Android : Can't create handler inside thread that has not called Looper.prepare()
- Android java.lang.RuntimeException: Can\'t create handler inside thread that has not called Looper.prepare()
- plsql returning bulk collect
- 杭电1708Fibonacci String
- 单点登录SSO的实现原理
- 快捷菜单
- Android Studio单元测试
- Android蓝牙开发,报BluetoothAdapter﹕ Can't create handler inside thread that has not called Looper.prepare
- ubuntu 13.04 安装 JDK
- -Dmaven.multiModuleProjectDirectory system propery is not set.【eclipse使用maven3.3插件进行编译的时候报错,按照里面的修改】
- 制作MAC和Windows同时支持的U盘和移动硬盘
- HDU 1255 覆盖的面积
- 栈的简单操作
- 三大框架的原理和优缺点
- Populating Next Right Pointers in Each Node
- CentOS 下如何修改 MySQL 的密码