如何在Android中使用sharedpreferences更改PIN码?

时间:2013-04-09 13:03:56

标签: java android

我正在使用共享首选项为应用程序引脚实现android演示,我输入引脚并确认引脚。问题是当我访问此值时,它没有返回更新的值,它给了我{{1} }。

NullPointerException

logcat的:

String strGetPin;
public final String pref_filename = "settings";
String myPassword;
static SharedPreferences shaedPref;
public static String strdataReturned = "";

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.settings_list);
  activity = this;
  edtxt_EnterPin = (EditText) findViewById(R.id.edtxt_Pin);
  edtxt_NewPin = (EditText) findViewById(R.id.edtxt_NewPin);
  edtxt_ConfirmPin = (EditText) findViewById(R.id.edtxt_ConfirmPin);
  shaedPref = getSharedPreferences(pref_filename, 0);
  strdataReturned = shaedPref.getString("user", "");
  if (strdataReturned.equals("")) {
    SharedPreferences.Editor editor = shaedPref.edit();
    editor.putString("user", "1234"); // newData is new pass,passwordKey is key
    editor.commit();
  }
  btnChangePin = (Button) findViewById(R.id.btn_ChangePin);

  btnChangePin.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
      // TODO Auto-generated method stub
      myDialog = new Dialog(Settings_Activity.this);
      myDialog.setContentView(R.layout.pin_change);
      myDialog.setTitle("Application PIN Change");
      myDialog.setCancelable(false);
      //Button For Change Pin save
      Button btnChange = (Button) myDialog.findViewById(R.id.btn_Save);

      btnChange.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
          // TODO Auto-generated method stub
          String stringData = edtxt_EnterPin.getText().toString().trim();
          strdataReturned = shaedPref.getString("user", ""); // key/def message
          // if stored password is equal to entered password
          if (strdataReturned.equals(stringData)) {
            String newData = edtxt_NewPin.getText().toString().trim();
            SharedPreferences.Editor editor = shaedPref.edit();
            editor.putString("user", newData); // newData is new pass, passwordKey is key
            editor.commit();
            Toast.makeText(getApplicationContext(), "PIN Change successfully", Toast.LENGTH_LONG).show();
            edtxt_EnterPin.setText("");
            edtxt_NewPin.setText("");
          } else {
            Toast.makeText(getApplicationContext(), "Wrong Pin!", Toast.LENGTH_LONG).show();
            edtxt_EnterPin.setText("");
            edtxt_NewPin.setText("");
          }
        }
      });
      Button btnCancel = (Button) myDialog.findViewById(R.id.btn_Cancel);

      btnCancel.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
          // TODO Auto-generated method stub
          myDialog.dismiss();
        }
      });
      myDialog.show();
    }
  });
}

1 个答案:

答案 0 :(得分:0)

在写入之前尝试清除SharedPreferences。

editor.clear();