Android使用Handler进行蓝牙数据传输

Android Bluetooth data transmission using Handler

作为 Android 的初学者,我有一个愚蠢的问题。我做的应用程序可以从蓝牙测量系统接收数据。数据传输效果很好,因为我可以在 Android Studio 的控制台中显示它,但我需要在循环中的一个活动中显示它。

蓝牙方法run()的部分代码class(我知道inputString,发送前已经转成int )

while (true) {
        in = new BufferedReader(new InputStreamReader(
                socket.getInputStream()));
        input = in.readLine();
        if (input.contains("+++")) {
            handler.obtainMessage(input).sendToTarget();
        }
    }

Activity 中的处理程序代码:

Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            textView.setText(message.what);
        }
};

之后应用程序因错误而崩溃:

FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: String resource ID #0x6

有人知道如何解决吗?

Resources$NotFoundException: String resource ID #0x6

由于:

textView.setText(message.what);

行。

参见 Message.what return int 类型的值,但 TextView.setText 需要 CharSequence 类型的值。

当我们将 int 值传递给 TextView.setText 方法时,系统将 int 值视为资源 ID,并在给定 int 没有可用资源时尝试查找它,然后它将通过 NotFoundException: String resource ID

在 TextView 中显示 int 值:

textView.setText(String.valueOf(message.what));

你也可以试试

textView.setText(message.what+"");