android: java.lang.NumberFormatException
One of my users has reported an error, this is the stack trace:
java.lang.RuntimeException: Unable to start service com.metatroid.minimal.weather.MinimalWeatherService@3082c088 with Intent { act=update dat=minimalweather://widget/id/295#update295 cmp=com.metatroid.minimal.weather/.MinimalWeatherService (has extras) }: java.lang.NumberFormatException:
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3063)
at android.app.ActivityThread.access$3600(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NumberFormatException:
at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:305)
at java.lang.Float.parseFloat(Float.java:291)
at java.lang.Float.valueOf(Float.java:330)
at com.metatroid.minimal.weather.MinimalWeatherService.onStartCommand(MinimalWeatherService.java:104)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
Line 104 is remoteView.setFloat(R.id.dateText, "setTextSize", Float.valueOf(dst.trim()).floatValue());
Where 'dst' is
SharedPreferences prefs = getApplicationContext().getSharedPreferences("prefs", 0);
String dst = prefs.getString("dsize", "10");
Which is set from a configure activity with:
SharedPreferences prefs = self.getSharedPreferences("prefs", 0);
SharedPreferences.Editor edit = prefs.edit();
EditText ds = (EditText) findViewById(R.id.dateSize);
String dsv = ds.getText().toString().trim();
edit.putString("dsize", dsv);
edit.commit();
finish();
And R.id.dateSize refers to:
<EditText android:id="@+id/dateSize"
android:layout_width="fill_parent"
android:layout_height="wrap_开发者_如何学编程content"
android:numeric="decimal"
android:maxLength="2"/>
I don't see how the user was able to input anything other than a decimal, or how this error might happen. Any ideas? Is there something wrong with the above code?
You should also check for a single period which will also cause parseFloat to give an exception.
if(!dsv.equals("") || dsv.equals("."))
I think you might have parsed an empty String.
String dsv = ds.getText().toString().trim();
if(!dsv.equals(""))
edit.putString("dsize", dsv);
精彩评论