0

林不知道我在做什麼錯。在我的應用程序中,我試圖讓它記住提交時輸入到文本框中的以前的密碼。從使用共享首選項進行的研究保存變量,即使應用程序重新啓動。頂我的代碼: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) 

不知道這次代碼有什麼問題,但任何事情都應該有所幫助。

回答

1

使用該代碼來獲得SharedPreferencefragment

SharedPreferences preferences = this.getActivity().getSharedPreferences("myPref", Context.MODE_PRIVATE); 
1

嘗試

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

你得到NullPointerException異常,因爲環境是不確定的。在調用context.getSharedPreference()之前定義它,或者使用其他方式,如getApplicationContext()。getSharedPreference()。

1

您是否定義了上下文?

Context context; 

我沒有在這裏看到上下文的值。 也許這就是爲什麼它返回的原因NullPointerException 你應該用活動或上下文來定義它。 你也可以做到這一點,而不是讓背景VAR: getApplicationContext().getSharedPreferences() 或者 getActivity().getSharedPreferences()

相關問題