-2

私はflick_level_number好みに合わせて保存しようとしています。
しかし、これは機能しません:(どうすればそれを保存できflick_level_numberますか?
そしてどうすればonCreateを読むことができますか?

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)
4

1 に答える 1

2

int を格納したい設定クラスに次のように言うだけです。

 editor.putInt("name", put your int value here);
 instead of
 editor.putString();

flick_level_number を文字列として保存することはできますが、文字列を 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();
于 2013-08-14T22:08:44.543 に答える