我在执行修改后的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
答案 0 :(得分:2)
很抱歉,我是android的新手,我不知道哪里可以看到价值为空。在289行中,如果得到了Handler函数,并且在这一行中完全如下:GlobalVar.mConversationArrayAdapter.clear();我在这条线上设置了一个breackpoint,我意识到这就是问题,正如你所说,问题在哪里,但我不知道如何解决。
所以mConversationArrayAdapter似乎没有定义。在BluetoothChat示例代码中,有:
mConversationArrayAdapter = new ArrayAdapter<String>(this, R.layout.message);
我认为您的活动BTActivity正在尝试使用BluetoothChat组件(在这种情况下您不应该这样做。)
答案 1 :(得分:1)
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)
您必须检查BTActivity行289行。
在那里放一个断点并从调试器开始。 您将看到哪个值为null。比容易修复。
请更新问题并标记该行。
希望它有所帮助!