After an Android Studio and SDK update, I can't run my app on either the emulator or the device. Always get the same error invalid float
.
MySettingsActivity.java:
public class MySettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
setContentView(R.layout.preference_screen);
addPreferencesFromResource(R.xml.preferences);
getListView().addHeaderView(LayoutInflater.from(this).inflate(R.layout.preference_header, null));
getListView().setSelector(getResources().getDrawable(R.drawable.listview_item_background));
getListView().setVerticalFadingEdgeEnabled(false);
getListView().setOverScrollMode(ListView.OVER_SCROLL_NEVER);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{
if (key.equals("touch_reaction"))
{
boolean touch_reaction = sharedPreferences.getBoolean("touch_reaction", true);
toast("Touch Reaction: ", touch_reaction ? "ON" : "OFF");
return;
}
}
private void toast(String text, String more)
{
View layout = getLayoutInflater().inflate(R.layout.toast, null);
TextView text_view = (TextView) layout.findViewById(R.id.text);
text_view.setText(text);
TextView more_view = (TextView) layout.findViewById(R.id.more);
more_view.setText(more);
if (more.equals(""))
{
((ViewManager) more_view.getParent()).removeView(more_view);
}
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
}
private void toast(String text)
{
toast(text, "");
}
}
Logcat:
--------- beginning of crash
06-11 22:27:48.017 6196-6196/starbox.connect E/AndroidRuntime: FATAL EXCEPTION: main
Process: starbox.connect, PID: 6196
java.lang.RuntimeException: Unable to start activity ComponentInfo{starbox.connect/starbox.connect.MySettingsActivity}:
java.lang.NumberFormatException: Invalid float: ""
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NumberFormatException: Invalid float: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseFloat(StringToReal.java:308)
at java.lang.Float.parseFloat(Float.java:306)
at starbox.connect.MyColorPreference.loadPersistedColor(MyColorPreference.java:185)
at starbox.connect.MyColorPreference.onSetInitialValue(MyColorPreference.java:127)
at android.preference.Preference.dispatchSetInitialValue(Preference.java:1385)
at android.preference.Preference.onAttachedToHierarchy(Preference.java:1182)
at android.preference.PreferenceGroup.addPreference(PreferenceGroup.java:164)
at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:105)
at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:104)
at android.preference.GenericInflater.rInflate(GenericInflater.java:490)
at android.preference.GenericInflater.rInflate(GenericInflater.java:495)
at android.preference.GenericInflater.inflate(GenericInflater.java:327)
at android.preference.GenericInflater.inflate(GenericInflater.java:264)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:273)
at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1521)
at starbox.connect.MySettingsActivity.onCreate(MySettingsActivity.java:27)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
In your loadPersistedColor()
-method, there should be a Float.parseFloat(String)
-call, where the string that you pass into that function is an empty string (""
). That is not a valid float-value.
The question is: Is that a valid situation? If it's (in your logic) okay for the string to be empty, simple check for an empty string and return your default value. If it's not valid in your logic, you might have a problem storing your data.