BluetoothChat ConnectedThread崩溃 [英] BluetoothChat ConnectedThread crash

查看:326
本文介绍了BluetoothChat ConnectedThread崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在执行修改BluetoothChat应用得到这个问题。该应用程序显示了设备,开始连接,然后崩溃。我一直在测试这和它发生在这个线程。

下面是code和LogCat中:

 公共类ConnectedThread继承Thread {
//调试
私有静态最后弦乐TAG =BluetoothChatService;
私有静态最终布尔D =真实的;公共ConnectedThread(插座的BluetoothSocket){
    Log.d(TAG,创造ConnectedThread);
    GlobalVar.mmSocket =插座;
    InputStream的tmpIn = NULL;
    OutputStream的tmpOut = NULL;    / **获取输入和输出流,使用临时对象,因为成员流是最后的* /
    尝试{
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    }赶上(IOException异常五){Log.e(TAG,临时插座没有创造,E); }    GlobalVar.mmInStream = tmpIn;
    GlobalVar.mmOutStream = tmpOut;
}@覆盖
公共无效的run(){
    Log.i(TAG,BEGIN mConnectedThread);
    字节[]缓冲区=新的字节[1024]; //缓冲存储区的流
    诠释字节; //个字节read()返回    / **继续听InputStream的,直到出现异常* /
    而(真){
        尝试{
            如果(D)Log.d(TAG,首发);
            / **从InputStream读取* /
            字节= GlobalVar.mmInStream.read(缓冲);            / **发送字节获得的UI活动* /
            GlobalVar.mHandler.obtainMessage(GlobalVar.MESSAGE_READ,字节,-1​​,缓冲液).sendToTarget();
        }赶上(IOException异常五){
            Log.e(TAG,断开,E);
            GlobalVar.mTransmission.connectionLost();            / **在启动服务重新启动监听模式* /
            如果(D)Log.d(TAG,降临);
            ConnectedThread.this.start(); //这可能是错误的!
            打破;
        }
    }
}
/ **
 *写到所连接的OutStream。
 * @参数缓冲区中写入的字节数
 * /
公共无效写入(字节[]缓冲区){
    尝试{
        GlobalVar.mmOutStream.write(缓冲液);        / **分享发送的消息回到UI活动* /
        GlobalVar.mHandler.obtainMessage(GlobalVar.MESSAGE_WRITE,-1,-1,缓冲液).sendToTarget();    }赶上(IOException异常五){}
}
/ **从主要活动调用此来关闭连接* /
公共无效取消(){
    尝试{
        GlobalVar.mmSocket.close();
    }赶上(IOException异常五){}
}

}

/这是LogCat中,其中,可以看出,它在当进入,然后崩溃

  12月7日至23日:22:55.560:D / AbsListView(27386):unregisterIRListener()被调用
12月7日至23日:22:55.590:D / BluetoothUtils(27386):isSocketAllowedBySecurityPolicy启动:设备空
12月7日至23日:22:55.590:所谓无BluetoothManagerCallback getBluetoothService():W / BluetoothAdapter(27386)
12月7日至23日:22:55.600:E / SpannableStringBuilder(27386):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能有长度为零
12月7日至23日:22:55.600:E / SpannableStringBuilder(27386):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能有长度为零
12月7日至23日:22:55.620:D / AbsListView(27386):onDetachedFromWindow
12月7日至23日:22:55.620:D / AbsListView(27386):unregisterIRListener()被调用
12月7日至23日:22:55.620:D / AbsListView(27386):onDetachedFromWindow
12月7日至23日:22:55.620:D / AbsListView(27386):unregisterIRListener()被调用
12月7日至23日:22:57.032:D / BluetoothChatService(27386):创建ConnectedThread
12月7日至23日:22:57.052:D / AndroidRuntime(27386):关闭VM
12月7日至23日:22:57.052:I / BluetoothChatService(27386):BEGIN mConnectedThread
12月7日至23日:22:57.052:D / BluetoothChatService(27386):首发
12月7日至23日:22:57.052:W / dalvikvm(27386):主题ID = 1:螺纹未捕获的异常(组= 0x41d58ac8)退出
12月7日至23日:22:57.052:E / AndroidRuntime(27386):致命异常:主要
12月7日至23日:22:57.052:E / AndroidRuntime(27386):显示java.lang.NullPointerException
12月7日至23日:22:57.052:E / AndroidRuntime(27386):在com.example.btaplication.BTActivity $ 1.handleMessage(BTActivity.java:289)
12月7日至23日:22:57.052:E / AndroidRuntime(27386):在android.os.Handler.dispatchMessage(Handler.java:99)
12月7日至23日:22:57.052:E / AndroidRuntime(27386):在android.os.Looper.loop(Looper.java:137)
12月7日至23日:22:57.052:E / AndroidRuntime(27386):在android.app.ActivityThread.main(ActivityThread.java:5328)
12月7日至23日:22:57.052:E / AndroidRuntime(27386):在java.lang.reflect.Method.invokeNative(本机方法)
12月7日至23日:22:57.052:E / AndroidRuntime(27386):在java.lang.reflect.Method.invoke(Method.java:511)
12月7日至23日:22:57.052:E / AndroidRuntime(27386):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102)
12月7日至23日:22:57.052:E / AndroidRuntime(27386):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
12月7日至23日:22:57.052:E / AndroidRuntime(27386):在dalvik.system.NativeStart.main(本机方法)
12月7日至23日:28:07.475:I /流程(27386):发送信号。 PID:27386 SIG:9


解决方案

  

很抱歉,但我在Android的新的,我不知道在哪里看到至极值都有空。在289线,如果得到了处理函数,并且正是在这条线以下:GlobalVar.mConversationArrayAdapter.clear();我在这一行设置一个breackpoint,我意识到,这是你说的是哪里的问题品脱,但我不知道如何解决。


所以mConversationArrayAdapter似乎没有定义。在BluetoothChat样本code,有:

  mConversationArrayAdapter =新ArrayAdapter<串GT;(这一点,R.layout.message);

我觉得你的活动BTActivity试图使用BluetoothChat组件(你不应该在这种情况下)。

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

解决方案

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).

这篇关于BluetoothChat ConnectedThread崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆