BluetoothChat ConnectedThread 崩溃

问题描述 投票:0回答:2

我在执行修改后的 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
android bluetooth chat
2个回答
2
投票

抱歉,我是 Android 新手,我不知道在哪里可以看到该值为空。在第289行,如果得到了Handler函数,恰好在这一行中如下: GlobalVar.mConversationArrayAdapter.clear();我在这条线上设置了一个断点,我意识到这就是你所说的问题所在,但我不知道如何解决。

所以 mConversationArrayAdapter 似乎没有定义。在BluetoothChat示例代码中,有:

        mConversationArrayAdapter = new ArrayAdapter<String>(this, R.layout.message);

我认为您的活动 BTActivity 正在尝试使用 BluetoothChat 组件(在这种情况下您不应该这样做)。


1
投票

07-23 12:22:57.052: E/AndroidRuntime(27386): java.lang.NullPointerException 07-23 12:22:57.052: E/AndroidRuntime(27386):位于 com.example.btaplication.BTActivity$1.handleMessage(BTActivity.java:289)

您必须检查 BTActivity 第 289 行。

在那里放置一个断点并启动调试器。 您将看到哪个值为空。比较容易修复。

请更新问题并标记该行。

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.