作为 Android 的初学者,我有一个愚蠢的问题。我做的应用程序可以从蓝牙测量系统接收数据。数据传输很酷,因为我可以在 Android Studio 的控制台中显示它,但我需要在循环中的一个 Activity 中显示它。
Bluetooth类run()
方法的部分代码(我知道input
是String
,我把它转成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返回 int
类型的值,但 TextView.setText
需要 CharSequence
类型的值。
当我们将 int
值传递给 TextView.setText
方法时,系统将 int 值视为资源 ID,并在给定 int 没有可用资源时尝试查找它,然后它会通过NotFoundException: String resource ID
在 TextView 中显示 int
值:
textView.setText(String.valueOf(message.what));
关于java - Android蓝牙数据传输使用Handler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33483159/