[英]Cannot convert String to Integer in Android Studio / Java
我有一个通过蓝牙接收数字(精确到毫秒)的功能。
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "UTF-8");
readBufferPosition = 0;
started = "";
int myInt;
if (data != "") {
try{
myInt = new Integer(data);
long seconds = TimeUnit.MILLISECONDS.toSeconds(myInt);
}catch(NumberFormatException ex){
System.err.println("NumberFormatException "+ ex.getMessage());
}
...
} else {
...
}
上面的尝试抛出:
NumberFormatException Invalid int: "8250
我认为接收到的数据仅包含毫秒数,但是当我这样做时,如何找到该数据呢?
myLabel.setText(data);
它告诉我: 8250
有任何想法吗?
您的data
是一个字符串。 当您稍后尝试形成Integer时,此操作将失败。 您需要使用以下代码: myInt = new Integer(Integer.parseInt(data));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.