android初学者。
我正试图在我的nexus 5上运行来自谷歌http://developer.android.com/samples/BluetoothLeGatt/index.html的蓝牙低能量样本。
我可以成功启动应用程序,扫描并显示可用的设备,但是当我尝试连接到其中一个时,我最终得到了一个java.lang.NullPointerException:
08-06 12:50:05.333: D/BluetoothAdapter(11840): stopLeScan()
08-06 12:50:05.353: W/ActivityManager(765): Unable to start service Intent { cmp=com.bledevicetest/.BluetoothLeService } U=0: not found
08-06 12:50:05.403: I/ActivityManager(765): Displayed com.bledevicetest/.DeviceControlActivity: +73ms
08-06 12:50:09.783: D/BluetoothAdapter(11840): stopLeScan()
08-06 12:50:14.483: D/AndroidRuntime(11840): Shutting down VM
08-06 12:50:14.483: W/dalvikvm(11840): threadid=1: thread exiting with uncaught exception (group=0x41ed4ba8)
08-06 12:50:14.483: E/AndroidRuntime(11840): FATAL EXCEPTION: main
08-06 12:50:14.483: E/AndroidRuntime(11840): Process: com.bledevicetest, PID: 11840
08-06 12:50:14.483: E/AndroidRuntime(11840): java.lang.NullPointerException
08-06 12:50:14.483: E/AndroidRuntime(11840): at com.bledevicetest.DeviceControlActivity.onOptionsItemSelected(DeviceControlActivity.java:217)
08-06 12:50:14.483: E/AndroidRuntime(11840): at android.app.Activity.onMenuItemSelected(Activity.java:2600)
08-06 12:50:14.483: E/AndroidRuntime(11840): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1012)
08-06 12:50:14.483: E/AndroidRuntime(11840): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:741)
08-06 12:50:14.483: E/AndroidRuntime(11840): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
08-06 12:50:14.483: E/AndroidRuntime(11840): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:884)
08-06 12:50:14.483: E/AndroidRuntime(11840): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
08-06 12:50:14.483: E/AndroidRuntime(11840): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:546)
08-06 12:50:14.483: E/AndroidRuntime(11840): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
08-06 12:50:14.483: E/AndroidRuntime(11840): at android.view.View.performClick(View.java:4438)
08-06 12:50:14.483: E/AndroidRuntime(11840): at android.view.View$PerformClick.run(View.java:18422)
08-06 12:50:14.483: E/AndroidRuntime(11840): at android.os.Handler.handleCallback(Handler.java:733)
08-06 12:50:14.483: E/AndroidRuntime(11840): at android.os.Handler.dispatchMessage(Handler.java:95)
08-06 12:50:14.483: E/AndroidRuntime(11840): at android.os.Looper.loop(Looper.java:136)
08-06 12:50:14.483: E/AndroidRuntime(11840): at android.app.ActivityThread.main(ActivityThread.java:5001)
08-06 12:50:14.483: E/AndroidRuntime(11840): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 12:50:14.483: E/AndroidRuntime(11840): at java.lang.reflect.Method.invoke(Method.java:515)
08-06 12:50:14.483: E/AndroidRuntime(11840): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-06 12:50:14.483: E/AndroidRuntime(11840): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-06 12:50:14.483: E/AndroidRuntime(11840): at dalvik.system.NativeStart.main(Native Method)
08-06 12:50:14.493: W/ActivityManager(765): Force finishing activity com.bledevicetest/.DeviceControlActivity
这可能来自哪些想法?
在第217行:mBluetoothLeService.connect(mDeviceAddress);
另外,我在尝试连接之前注意到了这个错误:
08-06 12:50:05.353:W / ActivityManager(765):无法启动服务Intent {cmp = com.bledevicetest / .BluetoothLeService} U = 0:未找到
这会与问题有关吗?
编辑:解决方案
我没有在清单中声明服务。那就是问题所在。 问题解决了!
答案 0 :(得分:0)
问题是我在清单文件中有:
<activity
android:name="com.bledevicetest.BluetoothLeService"
android:label="@string/title_activity_bluetooth_le_service" >
</activity>
所以,我改为:
<service
android:name="com.bledevicetest.BluetoothLeService"
android:enabled="true" >
</service>
并且有效。