简体   繁体   English

Sharedpreferences的微调器问题

[英]Spinner problems with Sharedpreferences

Sorry,this is my first time to put questions in this site 对不起,这是我第一次在这个网站上提问

My Function: I set two Buttons to restore and save the information in the Spinner and EditText with the function Sharedpreferences. 我的功能:我设置了两个按钮,以使用功能Sharedpreferences将信息还原并保存在Spinner和EditText中。

I execute the program at the first time. 我第一次执行程序。 The program will appear the error state,if I click the Button "restore" to restore the information in Spinner. 如果我单击“恢复”按钮以在Spinner中恢复信息,该程序将显示错误状态。 But I haven't met the problem when I restore the information in EditText. 但是,当我在EditText中还原信息时,我还没有遇到问题。

This is the code in Spinner 这是Spinner中的代码

private Spinner.OnItemSelectedListener getfeet = new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int position,
            long id) {
        // TODO Auto-generated method stub
        feet_out = parent.getSelectedItemPosition() + 2;
        select_f = feet.getSelectedItemPosition(); //save the position you choose
        Toast.makeText(MainActivity.this,
                "you chose " + parent.getSelectedItem().toString(),
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // TODO Auto-generated method stub
    }
};
private Spinner.OnItemSelectedListener getinch = new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int position,
            long id) {
        // TODO Auto-generated method stub
        inch_out = parent.getSelectedItemPosition();
        select_i = inch.getSelectedItemPosition(); //save the position you choose
        Toast.makeText(MainActivity.this,
                "you chose " + parent.getSelectedItem().toString(),
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // TODO Auto-generated method stub

    }

};

This is the code which executes the function of save in Sharedpreferences 这是执行Sharedpreferences中保存功能的代码

private void save_() {
    settings = getSharedPreferences("DATA", 0);
    settings.edit().putInt("DATA_FEET", select_f) //store the position in DATA_FEET and DATA_INCH
            .putInt("DATA_INCH", select_i)
            .putString("DATA_WEIGHT", weight.getText().toString()).commit();
    Toast.makeText(MainActivity.this, R.string.done, Toast.LENGTH_SHORT) //save done
            .show();
}

This is the code which executes the function of restore in Sharedpreferences 这是执行Sharedpreferences中的restore功能的代码

private void restore_() {
    feet.setSelection(settings.getInt("DATA_FEET", select_f)); //restore the position
    inch.setSelection(settings.getInt("DATA_INCH", select_i));
    weight.setText(settings.getString("DATA_WEIGHT", "EMPTY"));

}

My problem is that I can't use the function of restore at the program executing at the first time. 我的问题是我无法在第一次执行的程序中使用还原功能。 Is there any solution to solve the problem?? 有解决问题的办法吗? Because it is normal in EditText,but it is abnormal in Spinner. 因为在EditText中是正常的,但在Spinner中是异常的。 Thanks :)) 谢谢 :))

This is the state. 这是状态。 :)) :))

    10-23 23:14:11.677: D/TextLayoutCache(26370): Using debug level: 0 - Debug Enabled: 0
    10-23 23:14:11.747: D/libEGL(26370): loaded /system/lib/egl/libGLES_android.so
    10-23 23:14:11.797: D/libEGL(26370): loaded /system/lib/egl/libEGL_mali.so
    10-23 23:14:11.827: D/libEGL(26370): loaded /system/lib/egl/libGLESv1_CM_mali.so
    10-23 23:14:11.827: D/libEGL(26370): loaded /system/lib/egl/libGLESv2_mali.so
    10-23 23:14:11.887: D/OpenGLRenderer(26370): Enabling debug mode 0
    10-23 23:14:16.762: D/AndroidRuntime(26370): Shutting down VM
    10-23 23:14:16.762: W/dalvikvm(26370): threadid=1: thread exiting with uncaught exception (group=0x40aaa210)
    10-23 23:14:16.802: E/AndroidRuntime(26370): FATAL EXCEPTION: main
    10-23 23:14:16.802: E/AndroidRuntime(26370): java.lang.NullPointerException
    10-23 23:14:16.802: E/AndroidRuntime(26370):    at com.example.bmi.MainActivity.restore_(MainActivity.java:44)
    10-23 23:14:16.802: E/AndroidRuntime(26370):    at com.example.bmi.MainActivity.access$1(MainActivity.java:43)
    10-23 23:14:16.802: E/AndroidRuntime(26370):    at com.example.bmi.MainActivity$2.onClick(MainActivity.java:99)
    10-23 23:14:16.802: E/AndroidRuntime(26370):    at android.view.View.performClick(View.java:3574)
    10-23 23:14:16.802: E/AndroidRuntime(26370):    at android.view.View$PerformClick.run(View.java:14293)
    10-23 23:14:16.802: E/AndroidRuntime(26370):    at android.os.Handler.handleCallback(Handler.java:605)
    10-23 23:14:16.802: E/AndroidRuntime(26370):    at android.os.Handler.dispatchMessage(Handler.java:92)
    10-23 23:14:16.802: E/AndroidRuntime(26370):    at android.os.Looper.loop(Looper.java:137)
    10-23 23:14:16.802: E/AndroidRuntime(26370):    at android.app.ActivityThread.main(ActivityThread.java:4448)
    10-23 23:14:16.802: E/AndroidRuntime(26370):    at java.lang.reflect.Method.invokeNative(Native Method)
    10-23 23:14:16.802: E/AndroidRuntime(26370):    at java.lang.reflect.Method.invoke(Method.java:511)
    10-23 23:14:16.802: E/AndroidRuntime(26370):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
    10-23 23:14:16.802: E/AndroidRuntime(26370):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
    10-23 23:14:16.802: E/AndroidRuntime(26370):    at dalvik.system.NativeStart.main(Native Method)

This is the method to call the restore_() 这是调用restore_()的方法

private OnClickListener reback_1 = new OnClickListener() {
    public void onClick(View v) {
        restore_();
    }
};

After I replaced the select_f and select_i into 0 , it appeared the problem 在将select_fselect_i替换为0 ,出现了问题

10-24 00:01:30.957: D/TextLayoutCache(28836): Using debug level: 0 - Debug Enabled: 0
10-24 00:01:31.017: D/libEGL(28836): loaded /system/lib/egl/libGLES_android.so
10-24 00:01:31.037: D/libEGL(28836): loaded /system/lib/egl/libEGL_mali.so
10-24 00:01:31.057: D/libEGL(28836): loaded /system/lib/egl/libGLESv1_CM_mali.so
10-24 00:01:31.057: D/libEGL(28836): loaded /system/lib/egl/libGLESv2_mali.so
10-24 00:01:31.087: D/OpenGLRenderer(28836): Enabling debug mode 0
10-24 00:01:36.262: D/AndroidRuntime(28836): Shutting down VM
10-24 00:01:36.262: W/dalvikvm(28836): threadid=1: thread exiting with uncaught exception (group=0x40aaa210)
10-24 00:01:36.282: E/AndroidRuntime(28836): FATAL EXCEPTION: main
10-24 00:01:36.282: E/AndroidRuntime(28836): java.lang.NullPointerException
10-24 00:01:36.282: E/AndroidRuntime(28836):    at com.example.bmi.MainActivity.restore_(MainActivity.java:44)
10-24 00:01:36.282: E/AndroidRuntime(28836):    at com.example.bmi.MainActivity.access$1(MainActivity.java:43)
10-24 00:01:36.282: E/AndroidRuntime(28836):    at com.example.bmi.MainActivity$2.onClick(MainActivity.java:99)
10-24 00:01:36.282: E/AndroidRuntime(28836):    at android.view.View.performClick(View.java:3574)
10-24 00:01:36.282: E/AndroidRuntime(28836):    at android.view.View$PerformClick.run(View.java:14293)
10-24 00:01:36.282: E/AndroidRuntime(28836):    at android.os.Handler.handleCallback(Handler.java:605)
10-24 00:01:36.282: E/AndroidRuntime(28836):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 00:01:36.282: E/AndroidRuntime(28836):    at android.os.Looper.loop(Looper.java:137)
10-24 00:01:36.282: E/AndroidRuntime(28836):    at android.app.ActivityThread.main(ActivityThread.java:4448)
10-24 00:01:36.282: E/AndroidRuntime(28836):    at java.lang.reflect.Method.invokeNative(Native Method)
10-24 00:01:36.282: E/AndroidRuntime(28836):    at java.lang.reflect.Method.invoke(Method.java:511)
10-24 00:01:36.282: E/AndroidRuntime(28836):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-24 00:01:36.282: E/AndroidRuntime(28836):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-24 00:01:36.282: E/AndroidRuntime(28836):    at dalvik.system.NativeStart.main(Native Method)
10-24 00:01:37.803: I/Process(28836): Sending signal. PID: 28836 SIG: 9

You are using the SharedPrefs wrong. 您使用的SharedPrefs错误。

feet.setSelection(settings.getInt("DATA_FEET", select_f)); 

when you try to fetch the integer-value from sharedPref, you get returned null! 当您尝试从sharedPref获取整数值时,将返回null! because i think that you misunderstand how it is done: 因为我认为您误会了它的完成方式:

settings.getInt("myKey", defaultValue);

returns you the value for the key "myKey". 返回键“ myKey”的值。 if "myKey" has no value set, then it returns you the "defaultValue". 如果“ myKey”未设置任何值,则它将返回“ defaultValue”。 In your case it returns the current value of "select_f". 在您的情况下,它将返回“ select_f”的当前值。 And as it looks, the value of select_f is null, when you run your application for the first time. 看起来,第一次运行应用程序时select_f的值为null。

So you have to decide whether to initialize "select_f" before you retrieve the sharedPrefs or you enter another defaultValue here. 因此,您必须确定是要初始化“ select_f”,还是要检索sharedPrefs或在此处输入另一个defaultValue。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM