我在执行修改后的 BluetoothChat 应用程序时遇到了这个问题。该应用程序显示设备,开始连接,然后崩溃。我一直在测试这个,它发生在这个线程上。
这是代码和LogCat:
public class ConnectedThread extends Thread {
// Debugging
private static final String TAG = "BluetoothChatService";
private static final boolean D = true;
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread");
GlobalVar.mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
/**Get the input and output streams, using temp objects because member streams are final*/
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {Log.e(TAG, "temp sockets not created", e); }
GlobalVar.mmInStream = tmpIn;
GlobalVar.mmOutStream = tmpOut;
}
@Override
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
/**Keep listening to the InputStream until an exception occurs*/
while (true) {
try {
if (D) Log.d(TAG, "starting");
/**Read from the InputStream*/
bytes = GlobalVar.mmInStream.read(buffer);
/**Send the obtained bytes to the UI activity*/
GlobalVar.mHandler.obtainMessage(GlobalVar.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
GlobalVar.mTransmission.connectionLost();
/**Start the service over to restart listening mode*/
if (D) Log.d(TAG, "arrives");
ConnectedThread.this.start(); //This could be wrong!
break;
}
}
}
/**
* Write to the connected OutStream.
* @param buffer The bytes to write
*/
public void write(byte[] buffer) {
try {
GlobalVar.mmOutStream.write(buffer);
/**Share the sent message back to the UI Activity*/
GlobalVar.mHandler.obtainMessage(GlobalVar.MESSAGE_WRITE, -1, -1, buffer).sendToTarget();
} catch (IOException e) {}
}
/**Call this from the main activity to shutdown the connection*/
public void cancel() {
try {
GlobalVar.mmSocket.close();
} catch (IOException e) { }
}
}
/这是LogCat,可以看到它进入While然后崩溃。
07-23 12:22:55.560: D/AbsListView(27386): unregisterIRListener() is called
07-23 12:22:55.590: D/BluetoothUtils(27386): isSocketAllowedBySecurityPolicy start : device null
07-23 12:22:55.590: W/BluetoothAdapter(27386): getBluetoothService() called with no BluetoothManagerCallback
07-23 12:22:55.600: E/SpannableStringBuilder(27386): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-23 12:22:55.600: E/SpannableStringBuilder(27386): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-23 12:22:55.620: D/AbsListView(27386): onDetachedFromWindow
07-23 12:22:55.620: D/AbsListView(27386): unregisterIRListener() is called
07-23 12:22:55.620: D/AbsListView(27386): onDetachedFromWindow
07-23 12:22:55.620: D/AbsListView(27386): unregisterIRListener() is called
07-23 12:22:57.032: D/BluetoothChatService(27386): create ConnectedThread
07-23 12:22:57.052: D/AndroidRuntime(27386): Shutting down VM
07-23 12:22:57.052: I/BluetoothChatService(27386): BEGIN mConnectedThread
07-23 12:22:57.052: D/BluetoothChatService(27386): starting
07-23 12:22:57.052: W/dalvikvm(27386): threadid=1: thread exiting with uncaught exception (group=0x41d58ac8)
07-23 12:22:57.052: E/AndroidRuntime(27386): FATAL EXCEPTION: main
07-23 12:22:57.052: E/AndroidRuntime(27386): java.lang.NullPointerException
07-23 12:22:57.052: E/AndroidRuntime(27386): at com.example.btaplication.BTActivity$1.handleMessage(BTActivity.java:289)
07-23 12:22:57.052: E/AndroidRuntime(27386): at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 12:22:57.052: E/AndroidRuntime(27386): at android.os.Looper.loop(Looper.java:137)
07-23 12:22:57.052: E/AndroidRuntime(27386): at android.app.ActivityThread.main(ActivityThread.java:5328)
07-23 12:22:57.052: E/AndroidRuntime(27386): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 12:22:57.052: E/AndroidRuntime(27386): at java.lang.reflect.Method.invoke(Method.java:511)
07-23 12:22:57.052: E/AndroidRuntime(27386): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-23 12:22:57.052: E/AndroidRuntime(27386): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-23 12:22:57.052: E/AndroidRuntime(27386): at dalvik.system.NativeStart.main(Native Method)
07-23 12:28:07.475: I/Process(27386): Sending signal. PID: 27386 SIG: 9