我用蓝牙创建了一个应用程序,但是当蓝牙关闭时,apk关闭toast并且我有这个错误...
的 java.lang.NullPointerException
在 io.webbt.Activity2.openBT
void findBT()
{
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null)
{
myLabel.setText("No bluetooth adapter available");
}
if(!mBluetoothAdapter.isEnabled())
{
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, REQUEST_ENABLE_BT);
}
}
我有一条消息,如果我想要打开蓝牙我选择是......但是在没有继续我的活动之后...有什么帮助吗?如果蓝牙关闭我怎么回到我的活动?
01-16 15:36:08.483: W/dalvikvm(3677): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
01-16 15:36:08.483: E/AndroidRuntime(3677): FATAL EXCEPTION: main
01-16 15:36:08.483: E/AndroidRuntime(3677): java.lang.RuntimeException: Unable to start activity ComponentInfo{io.webbt/io.webbt.Activity2}: java.lang.NullPointerException
01-16 15:36:08.483: E/AndroidRuntime(3677): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
01-16 15:36:08.483: E/AndroidRuntime(3677): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-16 15:36:08.483: E/AndroidRuntime(3677): at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-16 15:36:08.483: E/AndroidRuntime(3677): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-16 15:36:08.483: E/AndroidRuntime(3677): at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 15:36:08.483: E/AndroidRuntime(3677): at android.os.Looper.loop(Looper.java:137)
01-16 15:36:08.483: E/AndroidRuntime(3677): at android.app.ActivityThread.main(ActivityThread.java:4441)
01-16 15:36:08.483: E/AndroidRuntime(3677): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 15:36:08.483: E/AndroidRuntime(3677): at java.lang.reflect.Method.invoke(Method.java:511)
01-16 15:36:08.483: E/AndroidRuntime(3677): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-16 15:36:08.483: E/AndroidRuntime(3677): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-16 15:36:08.483: E/AndroidRuntime(3677): at dalvik.system.NativeStart.main(Native Method)
01-16 15:36:08.483: E/AndroidRuntime(3677): Caused by: java.lang.NullPointerException
01-16 15:36:08.483: E/AndroidRuntime(3677): at io.webbt.Activity2.openBT(Activity2.java:192)
01-16 15:36:08.483: E/AndroidRuntime(3677): at io.webbt.Activity2.onStart(Activity2.java:130)
01-16 15:36:08.483: E/AndroidRuntime(3677): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
01-16 15:36:08.483: E/AndroidRuntime(3677): at android.app.Activity.performStart(Activity.java:4475)
01-16 15:36:08.483: E/AndroidRuntime(3677): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1940)
01-16 15:36:08.483: E/AndroidRuntime(3677): ... 11 more
void openBT() throws IOException
{
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standar
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
myLabel.setText("Bluetooth Opened");
protected void onStart() {
super.onStart();
try
{
//findBT();
openBT();
}
catch (IOException ex) { }
runnable.run();
}