java在Android中使用socket连接蓝牙时出现异常
我正在制作Android bluetooth应用程序,它可以打开或关闭bluetooth搜索设备、配对设备等,一切正常,但当我单击listview
时,连接设备上出现问题。出现项目异常:
Java.lang.NullpointerException
如何克服这种情况
xml
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
安卓:id="@+id/text"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceLarge"
安卓:text="@string/Text" />
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="horizontal"
安卓:layout_marginTop="30dp" >
<Button
安卓:id="@+id/turnOn"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/on" />
<Button
安卓:id="@+id/turnOff"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/off" />
</LinearLayout>
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="vertical"
安卓:layout_marginTop="80dp" >
<Button
安卓:id="@+id/paired"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/List" />
<Button
安卓:id="@+id/search"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/Find" />
<ListView
安卓:id="@+id/listView1"
安卓:layout_width="fill_parent"
安卓:layout_height="200dp" >
</ListView>
</LinearLayout>
</RelativeLayout>
Java代码
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 安卓.widget.AdapterView.performItemClick(AdapterView.java:284)
at 安卓.widget.ListView.performItemClick(ListView.java:3561)
at 安卓.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
at 安卓.os.Handler.handleCallback(Handler.java:587)
at 安卓.os.Handler.dispatchMessage(Handler.java:92)
at 安卓.os.Looper.loop(Looper.java:143)
at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.安卓.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);
出现异常
# 1 楼答案
让我们看看错误:
所以问题就在这里:
在try语句内的行上:
这意味着
device
必须为空。这意味着您正在将空值传递给ConnectThread
的构造函数。问题在于您在哪里创建BluetoothDevice对象