我正在尝试将flick_level_number
存储到偏好中
但这不起作用:(如何将其存储为flick_level_number
?
我怎样才能阅读创作?
public void selectSensibility(int level_number){
switch (level_number) {
case 0:
flick_width_sensibility = 0.2f;
flick_height_sensibility = 0.3f;
flick_level_number = 0;
break;
case 1:
flick_width_sensibility = 0.4f;
flick_height_sensibility = 0.2f;
flick_level_number = 1;
break;
case 2:
flick_width_sensibility = 0.6f;
flick_height_sensibility = 0.15f;
flick_level_number = 2;
break;
}
//Here, I want to store `flick_level_number` to Preference
Editor editor = sharedPref.edit();
editor.putString(
"flick_level_number", writeEditText.getText().toString());
editor.commit();
}
logcat的
08-14 22:05:34.102: E/AndroidRuntime(9413): FATAL EXCEPTION: main
08-14 22:05:34.102: E/AndroidRuntime(9413): java.lang.NullPointerException
08-14 22:05:34.102: E/AndroidRuntime(9413): at com.sample.MainActivity.selectSensibility(MainActivity.java:240)
08-14 22:05:34.102: E/AndroidRuntime(9413): at com.sample.MainActivity$2.onClick(MainActivity.java:270)
08-14 22:05:34.102: E/AndroidRuntime(9413): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:980)
08-14 22:05:34.102: E/AndroidRuntime(9413): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
08-14 22:05:34.102: E/AndroidRuntime(9413): at android.widget.AbsListView.performItemClick(AbsListView.java:1150)
08-14 22:05:34.102: E/AndroidRuntime(9413): at android.widget.ListView.performItemClick(ListView.java:4397)
08-14 22:05:34.102: E/AndroidRuntime(9413): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2985)
08-14 22:05:34.102: E/AndroidRuntime(9413): at android.widget.AbsListView$1.run(AbsListView.java:3671)
08-14 22:05:34.102: E/AndroidRuntime(9413): at android.os.Handler.handleCallback(Handler.java:615)
08-14 22:05:34.102: E/AndroidRuntime(9413): at android.os.Handler.dispatchMessage(Handler.java:92)
08-14 22:05:34.102: E/AndroidRuntime(9413): at android.os.Looper.loop(Looper.java:155)
08-14 22:05:34.102: E/AndroidRuntime(9413): at android.app.ActivityThread.main(ActivityThread.java:5511)
08-14 22:05:34.102: E/AndroidRuntime(9413): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 22:05:34.102: E/AndroidRuntime(9413): at java.lang.reflect.Method.invoke(Method.java:511)
08-14 22:05:34.102: E/AndroidRuntime(9413): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
08-14 22:05:34.102: E/AndroidRuntime(9413): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
08-14 22:05:34.102: E/AndroidRuntime(9413): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
只要对要存储int的首选项类说:
editor.putInt("name", put your int value here);
instead of
editor.putString();
您仍然可以将flick_level_number保存为String,只需将字符串解析为int:
myNum = Integer.parseInt(your preference that store your value);
并且onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// getpreferences
SharedPreferences settings = getSharedPreferences("Your preference file", 0);
final float value= settings.getFloat("flick_level_number", your default value);
final int value1 = settings.getInt("flick_level_number", your default value);
final String value2 = settings.getIString("flick_level_number", "your default value");
}
您的nullpointer是:sharedPref来解决此问题
sharedPref = getSharedPreferences("MyPrefsFile", 0);
Editor editor = sharedPref.edit();
editor.putInt("flick_level_number", flick_level_number);
editor.commit();