我的目标是获取一系列流数字并将其放入多维数组中。我确实意识到这可以在非多维数组中完成,但我的目标是最终扩展列数。但是,我得到一个错误NumberFormatException,我相信它来自parseDouble。有人可以解释我的错误吗?
请参阅下面的代码,其中包含LogCat错误。谢谢。
添加了log.d(“”,readMessage,null);
这是我的log.d的一部分。空白点是否为空?
04-04 21:19:33.196: D/(13272): 0.03
04-04 21:19:33.196: D/(13272): 0.
04-04 21:19:33.196: D/(13272):
04-04 21:19:33.196: D/(13272): 0.03
04-04 21:19:33.196: D/(13272): 0.
04-04 21:19:33.196: D/(13272):
04-04 21:19:33.196: D/(13272): 0.03
04-04 21:19:33.196: D/(13272): 0.
04-04 21:19:33.196: D/(13272):
case MESSAGE_READ:
for(int a= 0; a <30000; a++)
{
byte[] readBuf = (byte[]) msg.obj;
String readMessage = new String(readBuf, 0, msg.arg1);
mConversationArrayAdapter.add("Voltage: "+ readMessage);
double[] convert = new double[1];
for(int z=0; z <1;z++)
{
convert[z]= Double.parseDouble(readMessage);
}
for(int j=0; j<1;j++)
{
stored[a][j]= convert[a];
}
}
break;
logcat的
04-02 21:42:42.516: E/AndroidRuntime(10430): FATAL EXCEPTION: main
04-02 21:42:42.516: E/AndroidRuntime(10430): java.lang.NumberFormatException:
04-02 21:42:42.516: E/AndroidRuntime(10430): at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267)
04-02 21:42:42.516: E/AndroidRuntime(10430): at java.lang.Double.parseDouble(Double.java:318)
04-02 21:42:42.516: E/AndroidRuntime(10430): at com.example.android.BluetoothChat.BluetoothChat$2.handleMessage(BluetoothChat.java:305)
04-02 21:42:42.516: E/AndroidRuntime(10430): at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 21:42:42.516: E/AndroidRuntime(10430): at android.os.Looper.loop(Looper.java:130)
04-02 21:42:42.516: E/AndroidRuntime(10430): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-02 21:42:42.516: E/AndroidRuntime(10430): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 21:42:42.516: E/AndroidRuntime(10430): at java.lang.reflect.Method.invoke(Method.java:507)
04-02 21:42:42.516: E/AndroidRuntime(10430): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-02 21:42:42.516: E/AndroidRuntime(10430): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-02 21:42:42.516: E/AndroidRuntime(10430): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
readMessage变量应该只包含带有数字的文本,&#34; - &#34;标志或点,所以&#34; 121&#34;或&#34; 13.5&#34;或&#34; -5&#34;。如果有一个包含其他符号的字符串,则会发生NumberFormatException。只需看一下(例如Log.d),解析前的readMessage变量是什么