BluetoothChat ConnectedThread crash

♀尐吖头ヾ 提交于 2019-12-13 04:32:21

问题


I'm getting this problem while executing a modified BluetoothChat app. The app shows the devices, starts connecting to it, and then crashes. I've been testing this and it happens on this thread.

Here is the code and the 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) { }
}

}

/This is the LogCat where it can be seen that it enters in the While and then crashes.

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

回答1:


Sorry but I'm new at android and i don't know where to see wich value has null. In the 289 line, If got the Handler function, and exactly in this line the following: GlobalVar.mConversationArrayAdapter.clear(); I've set a breackpoint at this line and i realized that this is the pint where is the problem as you say, but I don't know how to solve.

So mConversationArrayAdapter seems to be not defined. In BluetoothChat sample code, there is :

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

I think your activity BTActivity is trying to use BluetoothChat component (And you should not in this case).




回答2:


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)

You have to check the BTActivity row 289 line.

Put a breakpoint there and start with debugger. You will see which value has null. Than will easy to fix.

Please update question and mark that line.

Hope it helps!



来源:https://stackoverflow.com/questions/17807629/bluetoothchat-connectedthread-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!