我正在使用共享首选项为应用程序 pin 实现 android 演示,我输入 pin 并确认 pin。问题是当我访问这个值时,它没有返回更新的值,它给了我NullPointerException
。
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();
}
});
}
日志猫:
04-09 18:42:12.694: D/(29235): onClick: Stoping Service
04-09 18:42:12.781: E/(29235): Service Stopped
04-09 18:42:19.861: D/dalvikvm(29235): GREF has increased to 201
04-09 18:42:50.712: D/AndroidRuntime(29235): Shutting down VM
04-09 18:42:50.712: W/dalvikvm(29235): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-09 18:42:50.721: E/AndroidRuntime(29235): FATAL EXCEPTION: main
04-09 18:42:50.721: E/AndroidRuntime(29235): java.lang.NullPointerException
04-09 18:42:50.721: E/AndroidRuntime(29235): at com.sms_alert.Settings_Activity$1$1.onClick(Settings_Activity.java:95)
04-09 18:42:50.721: E/AndroidRuntime(29235): at android.view.View.performClick(View.java:2485)
04-09 18:42:50.721: E/AndroidRuntime(29235): at android.view.View$PerformClick.run(View.java:9080)
04-09 18:42:50.721: E/AndroidRuntime(29235): at android.os.Handler.handleCallback(Handler.java:587)
04-09 18:42:50.721: E/AndroidRuntime(29235): at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 18:42:50.721: E/AndroidRuntime(29235): at android.os.Looper.loop(Looper.java:123)
04-09 18:42:50.721: E/AndroidRuntime(29235): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-09 18:42:50.721: E/AndroidRuntime(29235): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 18:42:50.721: E/AndroidRuntime(29235): at java.lang.reflect.Method.invoke(Method.java:507)
04-09 18:42:50.721: E/AndroidRuntime(29235): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-09 18:42:50.721: E/AndroidRuntime(29235): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-09 18:42:50.721: E/AndroidRuntime(29235): at dalvik.system.NativeStart.main(Native Method)
04-09 18:42:53.932: I/Process(29235): Sending signal. PID: 29235 SIG: 9