Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将字符串值解析为整数时出错_Java_Android_Parsing - Fatal编程技术网

Java 将字符串值解析为整数时出错

Java 将字符串值解析为整数时出错,java,android,parsing,Java,Android,Parsing,我从蓝牙获取数据,该数据是字符串类型,我试图在android studio中将该值解析为整数,我得到了错误“java.lang.NumberFormatException:Invalid int:”那么我该怎么解决它呢。 这是我的java代码: final Handler handler = new Handler(); final byte delimiter = 10; //This is the ASCII code for a newline character

我从蓝牙获取数据,该数据是字符串类型,我试图在android studio中将该值解析为整数,我得到了错误“java.lang.NumberFormatException:Invalid int:”那么我该怎么解决它呢。 这是我的java代码:

final Handler handler = new Handler();
        final byte delimiter = 10; //This is the ASCII code for a newline character

        stopWorker = false;
        readBufferPosition = 0;
        readBuffer = new byte[1024];
        workerThread = new Thread(new Runnable() {
            public void run() {
                while(!Thread.currentThread().isInterrupted() && !stopWorker) {
                    try {
                        int bytesAvailable = inputStream.available();
                        if (bytesAvailable > 0) {
                            byte[] packetBytes = new byte[bytesAvailable];
                            inputStream.read(packetBytes);
                            for (int i = 0; i < bytesAvailable; i++) {
                                byte b = packetBytes[i];
                                if (b == delimiter) {
                                    byte[] encodedBytes = new byte[readBufferPosition];
                                    System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                                    final String data = new String(encodedBytes, "US-ASCII");
                                    readBufferPosition = 0;

                                    handler.post(new Runnable() {
                                        public void run() {

                                            if(Integer.parseInt(data)<10) {//Here the error

                                                addNotification();
                                            }

                                            System.out.println(data);
                                        }
                                    });
                                } else {
                                    readBuffer[readBufferPosition++] = b;
                                }
                            }
                        }
                    } catch (IOException ex) {
                        stopWorker = true;
                    }
                }
            }
        });
final Handler=new Handler();
最后一个字节分隔符=10//这是换行符的ASCII码
stopWorker=false;
readBufferPosition=0;
readBuffer=新字节[1024];
workerThread=新线程(new Runnable()){
公开募捐{
而(!Thread.currentThread().isInterrupted()&&!stopWorker){
试一试{
int bytesavable=inputStream.available();
如果(字节可用>0){
byte[]packetBytes=新字节[bytesAvailable];
inputStream.read(packetBytes);
for(int i=0;i如果(Integer.parseInt(data),我不知道您在数据中接收到什么值。但我可以说,当您尝试将字符串转换为无效的Int时,会出现NumberFormatException。
例如:

字符串数据=“ABC”; 将其转换为整数时将引发异常。因为“ABC”不是整数

因此,您是否可以检查数据中的具体值? 另外,添加一个try-catch块

try{
    int i = Integer.parseInt(input);
} catch(NumberFormatException ex){ // handle your exception
    ...
}

使用
Integer.parseInt(String)
时,字符串数据必须只有数字或ASCII值
+
-
,否则会引发
NumberFormatException

公共静态int-parseInt(字符串s) 抛出NumberFormatException

将字符串参数解析为有符号十进制整数。字符 字符串中的所有字符都必须是十进制数字,但第一个字符除外 字符可以是ASCII减号“-”(“\u002D”)以表示 负值或ASCII加号“+”(“\u002B”)表示 正值


您似乎没有收到int值。请检查数据包含的内容,可能它包含一些意外字符。

将“数据”打印到日志中。然后您将发现我们的数据有什么问题。