我读了这么多文章,但仍然没有得到它。
我有一个String类型的串行数据,我想将它与整数进行比较。
这是读取的串行数据代码:
case MESSAGE_READ:
int i;
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
Log.i(LOG_TAG, readMessage);
mTextView.setText(readMessage);
String [] numbers = readMessage.split("\n");
int [] intNumber = new int[numbers.length];
//List<Integer> integers = new ArrayList<Integer>();
for (String number : numbers) {
//integers.add(Integer.valueOf(number.trim()));
for (i=0;i<numbers.length;i++){
intNumber[i] = Integer.valueOf(number.trim());
if (intNumber[i]<0 || intNumber[i]>95){
//Some Actions
}
}
}
break;
LogCat显示:
05-06 17:28:06.919: D/BluetoothReadService(15090): connected
05-06 17:28:06.919: D/BluetoothReadService(15090): create ConnectedThread
05-06 17:28:06.929: D/BluetoothReadService(15090): setState() 2 -> 3
05-06 17:28:06.949: I/BluetoothReadService(15090): BEGIN mConnectedThread
05-06 17:28:06.969: I/HBAS(15090): MESSAGE_STATE_CHANGE: 3
05-06 17:28:07.079: I/HBAS(15090): 24
05-06 17:28:07.079: I/HBAS(15090): 25
05-06 17:28:07.079: I/HBAS(15090): 26
05-06 17:28:07.079: I/HBAS(15090): 27
05-06 17:28:07.079: I/HBAS(15090): 28
05-06 17:28:07.079: I/HBAS(15090): 29
05-06 17:28:07.079: I/HBAS(15090): 0
05-06 17:28:07.079: I/HBAS(15090): 1
05-06 17:28:07.079: I/HBAS(15090): 2
05-06 17:28:07.079: I/HBAS(15090): 3
05-06 17:28:07.079: I/HBAS(15090): 4
05-06 17:28:07.079: I/HBAS(15090): 5
05-06 17:28:07.079: I/HBAS(15090): 6
05-06 17:28:07.079: I/HBAS(15090): 7
05-06 17:28:07.079: I/HBAS(15090): 8
05-06 17:28:07.079: I/HBAS(15090): 9
05-06 17:28:07.079: I/HBAS(15090): 10
05-06 17:28:07.079: I/HBAS(15090): 11
05-06 17:28:07.079: I/HBAS(15090): 12
05-06 17:28:07.079: I/HBAS(15090): 13
05-06 17:28:07.079: I/HBAS(15090): 14
05-06 17:28:07.079: I/HBAS(15090): 15
05-06 17:28:07.079: I/HBAS(15090): 16
05-06 17:28:07.079: I/HBAS(15090): 17
05-06 17:28:07.079: I/HBAS(15090): 18
05-06 17:28:07.079: I/HBAS(15090): 19
05-06 17:28:07.079: I/HBAS(15090): 20
05-06 17:28:07.079: I/HBAS(15090): 21
05-06 17:28:07.079: I/HBAS(15090): 22
05-06 17:28:07.079: I/HBAS(15090): 23
05-06 17:28:07.079: I/HBAS(15090): 24
05-06 17:28:07.079: I/HBAS(15090): 25
05-06 17:28:07.079: I/HBAS(15090): 26
05-06 17:28:07.079: I/HBAS(15090): 27
05-06 17:28:07.079: I/HBAS(15090): 28
05-06 17:28:07.079: I/HBAS(15090): 29
05-06 17:28:07.079: I/HBAS(15090): 0
05-06 17:28:07.079: I/HBAS(15090): 1
05-06 17:28:07.079: I/HBAS(15090): 2
05-06 17:28:07.079: I/HBAS(15090): 3
05-06 17:28:07.079: I/HBAS(15090): 4
05-06 17:28:07.079: I/HBAS(15090): 5
05-06 17:28:07.079: I/HBAS(15090): 6
05-06 17:28:07.079: I/HBAS(15090): 7
05-06 17:28:07.079: I/HBAS(15090): 8
05-06 17:28:07.079: I/HBAS(15090): 9
05-06 17:28:07.079: I/HBAS(15090): 10
05-06 17:28:07.079: I/HBAS(15090): 11
05-06 17:28:07.079: I/HBAS(15090): 12
05-06 17:28:07.079: I/HBAS(15090): 13
05-06 17:28:07.079: I/HBAS(15090): 14
05-06 17:28:07.079: I/HBAS(15090): 15
05-06 17:28:07.079: I/HBAS(15090): 16
05-06 17:28:07.079: I/HBAS(15090): 17
05-06 17:28:07.079: I/HBAS(15090): 18
05-06 17:28:07.079: I/HBAS(15090): 19
05-06 17:28:07.079: I/HBAS(15090): 20
05-06 17:28:07.079: I/HBAS(15090): 21
05-06 17:28:07.079: I/HBAS(15090): 22
05-06 17:28:07.079: I/HBAS(15090): 23
05-06 17:28:07.079: I/HBAS(15090): 24
05-06 17:28:07.079: I/HBAS(15090): 25
05-06 17:28:07.079: I/HBAS(15090): 26
05-06 17:28:07.079: I/HBAS(15090): 27
05-06 17:28:07.079: I/HBAS(15090): 28
05-06 17:28:07.079: I/HBAS(15090): 29
05-06 17:28:07.079: I/HBAS(15090): 0
05-06 17:28:07.079: I/HBAS(15090): 1
05-06 17:28:07.079: I/HBAS(15090): 2
05-06 17:28:07.079: I/HBAS(15090): 3
05-06 17:28:07.079: I/HBAS(15090): 4
05-06 17:28:07.079: I/HBAS(15090): 5
05-06 17:28:07.079: I/HBAS(15090): 6
05-06 17:28:07.079: I/HBAS(15090): 7
05-06 17:28:07.079: I/HBAS(15090): 8
05-06 17:28:07.079: I/HBAS(15090): 9
05-06 17:28:07.079: I/HBAS(15090): 10
05-06 17:28:07.079: I/HBAS(15090): 11
05-06 17:28:07.079: I/HBAS(15090): 12
05-06 17:28:07.079: I/HBAS(15090): 13
05-06 17:28:07.079: I/HBAS(15090): 14
05-06 17:28:07.079: I/HBAS(15090): 15
05-06 17:28:07.079: I/HBAS(15090): 16
05-06 17:28:07.079: I/HBAS(15090): 17
05-06 17:28:07.079: I/HBAS(15090): 18
05-06 17:28:07.079: I/HBAS(15090): 19
05-06 17:28:07.079: I/HBAS(15090): 20
05-06 17:28:07.079: I/HBAS(15090): 21
05-06 17:28:07.079: I/HBAS(15090): 22
05-06 17:28:07.079: I/HBAS(15090): 23
05-06 17:28:07.079: I/HBAS(15090): 24
05-06 17:28:07.079: I/HBAS(15090): 25
05-06 17:28:07.079: I/HBAS(15090): 26
05-06 17:28:07.079: I/HBAS(15090): 27
05-06 17:28:07.079: I/HBAS(15090): 28
05-06 17:28:07.079: I/HBAS(15090): 29
05-06 17:28:07.079: I/HBAS(15090): 0
05-06 17:28:07.079: I/HBAS(15090): 1
05-06 17:28:07.079: I/HBAS(15090): 2
05-06 17:28:07.079: I/HBAS(15090): 3
05-06 17:28:07.079: I/HBAS(15090): 4
05-06 17:28:07.079: I/HBAS(15090): 5
05-06 17:28:07.079: I/HBAS(15090): 6
05-06 17:28:07.079: I/HBAS(15090): 7
05-06 17:28:07.079: I/HBAS(15090): 8
05-06 17:28:07.079: I/HBAS(15090): 9
05-06 17:28:07.079: I/HBAS(15090): 10
05-06 17:28:07.079: I/HBAS(15090): 11
05-06 17:28:07.079: I/HBAS(15090): 12
05-06 17:28:07.969: I/HBAS(15090): 1
05-06 17:28:08.029: I/HBAS(15090): 3
05-06 17:28:09.019: I/HBAS(15090): 14
05-06 17:28:09.979: I/HBAS(15090): 1
05-06 17:28:10.029: I/HBAS(15090): 5
05-06 17:28:10.989: I/HBAS(15090): 16
05-06 17:28:12.009: I/HBAS(15090): 17
05-06 17:28:12.999: I/HBAS(15090): 18
05-06 17:28:13.999: I/HBAS(15090): 19
05-06 17:28:14.999: I/HBAS(15090): 20
05-06 17:28:16.009: I/HBAS(15090): 21
05-06 17:28:17.009: I/HBAS(15090): 22
05-06 17:28:18.009: I/HBAS(15090): 23
05-06 17:28:19.019: I/HBAS(15090): 24
05-06 17:28:20.019: I/HBAS(15090): 25
05-06 17:28:21.029: I/HBAS(15090): 26
05-06 17:28:22.029: I/HBAS(15090): 27
05-06 17:28:23.029: I/HBAS(15090): 28
05-06 17:28:24.039: I/HBAS(15090): 29
05-06 17:28:25.039: I/HBAS(15090): 0
05-06 17:28:26.029: I/HBAS(15090): 1
05-06 17:28:26.069: I/HBAS(15090):
05-06 17:28:26.069: D/AndroidRuntime(15090): Shutting down VM
05-06 17:28:26.069: W/dalvikvm(15090): threadid=1: thread exiting with uncaught exception (group=0x40018578)
05-06 17:28:26.079: E/AndroidRuntime(15090): FATAL EXCEPTION: main
05-06 17:28:26.079: E/AndroidRuntime(15090): java.lang.NumberFormatException: unable to parse '' as integer
05-06 17:28:26.079: E/AndroidRuntime(15090): at java.lang.Integer.parseInt(Integer.java:362)
05-06 17:28:26.079: E/AndroidRuntime(15090): at java.lang.Integer.parseInt(Integer.java:332)
05-06 17:28:26.079: E/AndroidRuntime(15090): at java.lang.Integer.valueOf(Integer.java:506)
05-06 17:28:26.079: E/AndroidRuntime(15090): at com.android.hbas.FinalSetting$1.handleMessage(FinalSetting.java:362)
05-06 17:28:26.079: E/AndroidRuntime(15090): at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 17:28:26.079: E/AndroidRuntime(15090): at android.os.Looper.loop(Looper.java:130)
05-06 17:28:26.079: E/AndroidRuntime(15090): at android.app.ActivityThread.main(ActivityThread.java:3687)
05-06 17:28:26.079: E/AndroidRuntime(15090): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 17:28:26.079: E/AndroidRuntime(15090): at java.lang.reflect.Method.invoke(Method.java:507)
05-06 17:28:26.079: E/AndroidRuntime(15090): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-06 17:28:26.079: E/AndroidRuntime(15090): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-06 17:28:26.079: E/AndroidRuntime(15090): at dalvik.system.NativeStart.main(Native Method)
一开始它起作用,但在某个时候它是FC因为''的事情。 任何解决方案??? 谢谢你的建议..
答案 0 :(得分:2)
检查您的字符串是否仅为' '
。
if(str.trim().length>0){
try{
int i =Integer.parseInt(str);
}catch(NumberFormatException ne){
System.out.println("could not parse :: " +ne);
}
}