Android应用中的SharedPreferences用于记住我的按钮

时间:2017-09-13 16:38:25

标签: javascript android plugins sharedpreferences android-sharedpreferences

我不确定我做错了什么。在我的应用程序中,我试图记住在提交时输入文本字段的先前密码。从使用共享首选项完成的研究中保存变量,即使应用程序重新启动也是如此。我的代码顶部:

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");
    }

然后我的代码将String保存在提交:

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)

这次不确定代码是否有问题,但任何事都应该有所帮助。

3 个答案:

答案 0 :(得分:1)

Context context = getApplicationContext();
SharedPreferences settings = context.getSharedPreferences(PASSVALUE, context.MODE_PRIVATE);

您正在获取NullPointerException,因为上下文未定义。在调用context.getSharedPreference()之前定义它,或者使用另一种方法,例如getApplicationContext()。getSharedPreference()。

答案 1 :(得分:1)

你定义了上下文吗?

Context context;

我在这里没有看到上下文的价值。 也许这就是它返回NullPointerException的原因 你应该用活动或上下文来定义它。 你也可以这样做而不是上下文var: getApplicationContext().getSharedPreferences() 要么 getActivity().getSharedPreferences()

答案 2 :(得分:1)

使用此代码在angular.module('application', []) .directive("navigation", ['$compile',function ($compile) { return { restrict: 'E', replace: true, scope: { menu: '=' }, template: '<ul><li ng-repeat="item in menu">{{item.Name}}<span ng-repeat="(key,value) in item.Children"><navigation menu="value"></navigation></span></li></ul>', compile: function (el) { var contents = el.contents().remove(); return function(scope,el){ $compile(contents)(scope,function(clone){ el.append(clone); }); }; } }; }]) .controller('myController', ['$scope','$log',function ($scope,$log) { $log.log('In Controller'); $scope.menu = [{ Id: 1, Name: "Contact", Children: {'user1': [{ Name: "Testing", Children: {} },{ Name: "More Testing", Children: {'user3': [{ Name: '3rd Level', Children: {} }]} }], 'user2': [{ Name: '3rd Level', Children: {} }]} }]; }]);

中获取SharedPreference
fragment