抱歉,这是我第一次在本网站提问
我的功能:我设置了两个按钮来恢复并使用Sharedpreferences功能将信息保存在Spinner和EditText中。
我第一次执行程序。如果我单击按钮“恢复”以恢复Spinner中的信息,程序将出现错误状态。但是当我在EditText中恢复信息时,我还没有遇到问题。
这是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
}
};
这是在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();
}
这是在Sharedpreferences
中执行还原功能的代码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"));
}
我的问题是我无法在第一次执行的程序中使用还原功能。有没有解决问题的方法?因为它在EditText中是正常的,但在Spinner中是异常的。谢谢:))
这是州。 :))
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)
这是调用restore _()
的方法private OnClickListener reback_1 = new OnClickListener() {
public void onClick(View v) {
restore_();
}
};
我将select_f
和select_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
答案 0 :(得分:0)
您使用的是SharedPrefs错误。
feet.setSelection(settings.getInt("DATA_FEET", select_f));
当您尝试从sharedPref获取整数值时,返回null!因为我认为你误解了它是如何完成的:
settings.getInt("myKey", defaultValue);
返回键“myKey”的值。如果“myKey”没有设置值,则返回“defaultValue”。在您的情况下,它返回“select_f”的当前值。当它看起来时,当你第一次运行你的应用程序时,select_f的值为null。
因此,您必须在检索sharedPrefs之前决定是否初始化“select_f”,或者在此处输入另一个defaultValue。