我正在制作Android蓝牙应用程序,它可以打开或关闭蓝牙搜索设备配对设备等所有工作正常但是当我点击listview
项目异常发生时连接设备上出现问题:
显示java.lang.NullPointerException
如何克服这种情况?
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/Text" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="30dp" >
<Button
android:id="@+id/turnOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/on" />
<Button
android:id="@+id/turnOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/off" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="80dp" >
<Button
android:id="@+id/paired"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/List" />
<Button
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Find" />
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="200dp" >
</ListView>
</LinearLayout>
</RelativeLayout>
JavaCode
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);//Exception
} catch (IOException e) { }
mmSocket = tmp;
}
logcat的
04-19 20:45:53.353 10902-10902/com.example.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x400205a0)
04-19 20:45:53.363 10902-10902/com.example.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.app.ConnectThread.<init>(ConnectThread.java:23)
at com.example.app.MainActivity.connect(MainActivity.java:124)
at com.example.app.MainActivity.access$000(MainActivity.java:30)
at com.example.app.MainActivity$5.onItemClick(MainActivity.java:117)
at android.widget.AdapterView.performItemClick(AdapterView.java:284)
at android.widget.ListView.performItemClick(ListView.java:3561)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4277)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
04-19 20:45:53.373 125-254/? W/ActivityManager﹕ Force finishing activity com.example.app/.MainActivity
04-19 20:45:53.873 125-140/? W/ActivityManager﹕ Activity pause timeout for HistoryRecord{407b6608 com.example.app/.MainActivity}
04-19 20:45:53.933 234-234/? I/CarouselTimeLog﹕ onStart() finish: 1429458353942
我正在制作蓝牙应用程序,一切顺利,直到我点击ListView
,tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
处有例外
答案 0 :(得分:0)
让我们看一下错误:
java.lang.NullPointerException
at com.example.app.ConnectThread.<init>(ConnectThread.java:23)
问题出在这里:
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);//Exception
} catch (IOException e) { }
mmSocket = tmp;
}
在try语句中的行上:
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
这意味着device
必须为null。这意味着您将空值传递给ConnectThread
的构造函数。问题出在您创建BluetoothDevice对象的位置。