在Android Studio和SDK更新后,我无法在模拟器或设备上运行我的应用。始终得到相同的错误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)
答案 0 :(得分:2)
在loadPersistedColor()
- 方法中,应该有Float.parseFloat(String)
- 调用,其中传递给该函数的字符串是空字符串(""
)。这不是有效的浮点值。
问题是:这是有效的情况吗?如果它(在你的逻辑中)可以使字符串为空,只需检查一个空字符串并返回默认值。如果它在您的逻辑中无效,则可能在存储数据时遇到问题。