0

我有一个名为DataInputstreamThread. DataInputstreamThread从蓝牙设备获取输入。

我想textbox使用 a 将处理后的数据从输入流添加到 a runOnUiThread。但这不起作用。runOnUiThread每次都被跳过。

代码::

public void getDataFromInputStream() {

    DataInputstreamThread = new Thread(new Runnable() {
        public void run() {


            threadFlag = connectDevice();
            while (threadFlag) {
                try {
                    if (inputStream == null) {
                        inputStream = bluetoothSocket.getInputStream();
                    }

                    bytesAvailable = inputStream.available();
                    if (bytesAvailable > 0) {
                        byte[] packetBytes = new byte[bytesAvailable];
                        inputStream.read(packetBytes);

                        for (int i = 0; i < bytesAvailable; i++) {

                            if (packetBytes[i] != 13) {
                                temp = new String(packetBytes);

                            } else {
                                delimiter = true;
                            }

                        }

                        fin += temp;//I have a breakpoint here, and i know this is executed


                        activity.runOnUiThread(new Runnable() {
                            public void run() {
                             notifyObservers(fin);//I have breakpoint here, and this line is not executed.
                            }
                        });

                        if (delimiter) {

                            fin = "";
                            delimiter = false;
                        }

                    }


                } catch (Exception e) {

                }

            }

            nullify();
        }

    });
    DataInputstreamThread.start();
}

那么为什么runOnUiThread没有被执行呢?

4

2 回答 2

3

这并不直接回答您提出的问题,但它解释了为什么您无法找到问题的原因。

这个:

catch (Exception e) {

}

正在摧毁您在找到错误时可能存在的任何希望。你吞下了异常,所以你看不到runOnUiThread可能抛出的错误。你只能猜测。

在那里做一些有用的事情。至少,将异常堆栈跟踪输出到 LogCat。如果之后,您仍然无法找到错误,请返回此处并扩展您的问题。

于 2013-09-09T09:23:38.493 回答
0

试试这个方法

Handler mHandler = new Handler();

        mHandler.post(new Runnable() {

            @Override
            public void run() {
                 notifyObservers(fin);
            }
        });

代替

activity.runOnUiThread(new Runnable() {
                            public void run() {
                             notifyObservers(fin);//I have breakpoint here, and this line is not executed.
                            }
                        });
于 2013-09-09T09:28:55.250 回答