Android应用程序中的“记住我"的SharedPreferences按钮 [英] SharedPreferences in Android app for remember me button
本文介绍了Android应用程序中的“记住我"的SharedPreferences按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不确定自己在做什么错.在我的应用程序中,我试图让它记住提交时在文本字段中输入的先前密码.根据使用共享首选项进行的研究,即使应用重新启动,也可以保存该变量.我的代码顶部:
Im not to sure what I'm doing wrong. In my app I'm trying to have it remember a previous password entered into the textfield on submit. From research done using shared preferences saving the variable even if the app it restarted. Top of my code:
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;
要在设置时检索文本字段:
To retrieve textfield when set:
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");
}
然后我的代码在提交时保存字符串:
Then my code to save the String on submit:
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中却收到此错误:
However overtime I run the plugin within my app I receive this error in the 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)
不能确定这次代码有什么问题,但是应该有帮助.
Not to sure whats wrong with the code this time but anything should help.
推荐答案
使用此代码在fragment
SharedPreferences preferences = this.getActivity().getSharedPreferences("myPref", Context.MODE_PRIVATE);
这篇关于Android应用程序中的“记住我"的SharedPreferences按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文