android - 字符串的子字符串问题并将其显示到 textView
问题描述
我有这个完美运行的代码
UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() { //Defining a Callback which triggers whenever data is read.
@Override
public void onReceivedData(byte[] arg0) {
String data = null;
try {
data = new String(arg0, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
tvAppend(textView, data);
}
};
我正在尝试对字符串数据进行子串化并将其显示到 textView 但我的应用程序崩溃了。我不知道为什么。它关于数据字符串的编码?
解决方案
可能存在 byte[] 到 String 转换失败的情况。
我建议你更换
String data = null;
至
String data = "";
还要添加崩溃日志。