gpt4 book ai didi

android - 操作和使用从 Arduino USB 获取的字符串

转载 作者:行者123 更新时间:2023-11-30 05:06:17 25 4
gpt4 key购买 nike

当它是原始格式时,我可以得到来自 USB 串口的字符串。但是一旦我尝试使用 Double.valueOf 或其他方法将其转换为数字,应用程序就会崩溃。

public void onReceivedData(byte[] arg0) {
String data = null;
try {
data = new String(arg0, "UTF-8");

Double waterLevel=Double.valueOf(data);

tvAppend(textView, waterLevel+"m");

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}


}

上面的方法在使用它并且只尝试将 String 转换为 Double 或 Float 时会产生问题:

   String waterLevel=data+"m";

错误信息:

2019-02-05 19:02:06.746 915-5126/? E/AwareLog: CPUFeatureAMSCommunicator: set app boost but type is unknown
2019-02-05 19:02:06.824 421-450/? E/DynamicFpsPolicy: FpsInfo: 1a500000000 can not find FpsPolicyService
2019-02-05 19:02:07.683 915-1498/? E/TouchFilter: setTouchFilter LOG Enable prameter: 0
2019-02-05 19:02:08.237 421-450/? E/DynamicFpsPolicy: FpsInfo: 1a500000000 can not find FpsPolicyService
2019-02-05 19:02:08.454 12270-12270/com.jorc.loggergeneral.jorclogger E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
2019-02-05 19:02:08.455 12270-12270/com.jorc.loggergeneral.jorclogger E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
2019-02-05 19:02:09.432 12270-12467/? E/AndroidRuntime: FATAL EXCEPTION: Thread-6
Process: com.jorc.loggergeneral.jorclogger, PID: 12270
java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:539)
at java.lang.Double.valueOf(Double.java:503)
at com.jorc.loggergeneral.jorclogger.MainActivity$1.onReceivedData(MainActivity.java:55)
at com.felhr.usbserial.UsbSerialDevice$WorkerThread.onReceivedData(UsbSerialDevice.java:209)
at com.felhr.usbserial.UsbSerialDevice$WorkerThread.run(UsbSerialDevice.java:184)
2019-02-05 19:02:09.483 915-1082/? E/InputDispatcher: channel '4af7ed1 com.jorc.loggergeneral.jorclogger/com.jorc.loggergeneral.jorclogger.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-02-05 19:02:09.535 421-450/? E/DynamicFpsPolicy: FpsInfo: 1a500000000 can not find FpsPolicyService
2019-02-05 19:02:09.706 915-1284/? E/TouchFilter: setTouchFilter LOG Enable prameter: 0
2019-02-05 19:02:09.938 1135-1135/? E/FullInputEventModel: onStartInput event aborted: ejh: could not obtain extracted text (class ejh)
2019-02-05 19:02:10.976 485-485/? E/Thermal-daemon: [battery] temp_new :32 temp_old :31
2019-02-05 19:02:10.979 485-485/? E/Thermal-daemon: Report temperature: [battery] temp :32 report_threshold:1
2019-02-05 19:02:11.731 915-1284/? E/TouchFilter: setTouchFilter LOG Enable prameter: 0

最佳答案

您的答案在堆栈跟踪中。您尝试解析一个空字符串。找个时间尝试使用调试器...

关于android - 操作和使用从 Arduino USB 获取的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54530003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com