SharedPreferences在Android应用程序记住我按钮
问题描述:
林不知道我在做什么错。在我的应用程序中,我试图让它记住提交时输入到文本框中的以前的密码。从使用共享首选项进行的研究保存变量,即使应用程序重新启动。顶我的代码:SharedPreferences在Android应用程序记住我按钮
public class AK_Voucher_access extends BT_fragment{
View thisScreensView = null;
String errormessage = "";
String errormessageTextColor = "";
String errormessageText = "";
String rememberTextText = "";
String rememberTextTextColor = "";
String voucherTextfieldPlaceholder = "";
String voucherTextfieldSecureTextEntry = "";
boolean voucherTextfieldSecureTextEntryBool = false;
String iphoneImage = "";
String ipadImage = "";
String errormessageInvalid = "";
String errormessageRemember = "";
private TextView errormessageTextView = null;
private EditText voucherTextfieldEditText = null;
private ImageView imageView = null;
private Button submitButton = null;
private Switch rememberSwitch = null;
private Drawable myHeaderImageDrawable = null;
private String alphaImage = "", pass;
private static BT_item screenObjectToLoad = null;
private static final String PASSVALUE = "value";
//private static final String appPassword = "appPassword";
Context context;
要检索文本框时设定:
if (rememberSwitch.isChecked()) {
BT_debugger.showIt(fragmentName + ":rememberSwitch is ON");
SharedPreferences settings = context.getSharedPreferences(PASSVALUE, MODE_PRIVATE);
String rpass= settings.getString("rpassword","");
if (rpass!=null) {
voucherTextfieldEditText.setText(rpass);
} else {
voucherTextfieldEditText.setText("nono");
}
}
else {
BT_debugger.showIt(fragmentName + ":rememberSwitch is OFF");
// switchStatus.setText("Switch is currently OFF");
}
然后我的代码,以节省提交的字符串:
if (loadScreenNickname.length() > 1 && !loadScreenNickname.equalsIgnoreCase("none")) {
// before we launch the next screen...
if (!rememberSwitch.isChecked()) {
voucherTextfieldEditText.setText("");
}
if (rememberSwitch.isChecked()){
//voucherTextfieldEditText.setText("");
String pass = voucherTextfieldEditText.getText().toString();
BT_debugger.showIt(pass + ":CODE");
SharedPreferences settings = context.getSharedPreferences(PASSVALUE, MODE_PRIVATE);
settings.edit().putString("rpassword", pass).apply();
rememberSwitch.setChecked(true);
}
errormessageTextView.setVisibility(View.GONE);
errormessageTextView.invalidate();
//loadScreenObject(null, thisScreen);
try {
loadScreenWithNickname(loadScreenNickname);
foundIt = 1;
} catch (Exception ex){
Log.e ("eTutorPrism Error", "Caught this exception " + ex);
ex.printStackTrace();
}
break;
}
}
但是加班我运行中的插件我应用程序我在logcat中收到此错误:
E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
at com.asfapp.ui.bt_plugins.AK_Voucher_access.onCreateView(AK_Voucher_access.java:210)
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.asfapp, PID: 24771
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
at com.asfapp.ui.bt_plugins.AK_Voucher_access.onCreateView(AK_Voucher_access.java:210)
不知道这次代码有什么问题,但任何事情都应该有所帮助。
答
使用该代码来获得SharedPreference
在fragment
SharedPreferences preferences = this.getActivity().getSharedPreferences("myPref", Context.MODE_PRIVATE);
答
尝试
Context context = getApplicationContext();
SharedPreferences settings = context.getSharedPreferences(PASSVALUE, context.MODE_PRIVATE);
你得到NullPointerException异常,因为环境是不确定的。在调用context.getSharedPreference()之前定义它,或者使用其他方式,如getApplicationContext()。getSharedPreference()。
答
您是否定义了上下文?
Context context;
我没有在这里看到上下文的值。 也许这就是为什么它返回的原因NullPointerException 你应该用活动或上下文来定义它。 你也可以做到这一点,而不是让背景VAR: getApplicationContext().getSharedPreferences()
或者 getActivity().getSharedPreferences()