我在开始我的活动时收到 NullPointerException 错误。我的 logcat 说它发生在我的活动中,但没有指定在哪里。我第一次尝试使用 SharedPreferences 并且不知道错误发生在哪里。
这是我的活动:
public class SharedPrefLoginActivity extends Activity {
private Button cancelBttn;
private EditText rname;
private EditText rpwrd;
private Button rBttn;
/** Called when the activity is first created. */
private SharedPreferences dhj;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dhj = this.getSharedPreferences("DHJ", MODE_WORLD_READABLE);
if(dhj.getString("password", null) !=null) {
setContentView(R.layout.register);
rname = (EditText) findViewById(R.id.reg_uname);
rpwrd = (EditText) findViewById(R.id.reg_pswd);
rBttn = (Button) findViewById(R.id.reg_button);
rBttn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SharedPreferences.Editor dhjEditor = dhj.edit();
dhjEditor.putString("rname", rname.getText().toString());
dhjEditor.putString("rpwrd", rpwrd.getText().toString());
dhjEditor.commit();
Toast.makeText(getApplicationContext(), "Credentials Saved!", Toast.LENGTH_SHORT).show();
}
}
);
}
else {
Intent i = new Intent(SharedPrefLoginActivity.this, AccessApp.class);
startActivity(i);
}
cancelBttn.setOnClickListener (new OnClickListener() {
@Override
public void onClick(View v) {
//Close application
finish();
}
});
}}
日志猫:
09-14 09:27:26.244: E/AndroidRuntime(589): Uncaught handler: thread main exiting due to uncaught exception
09-14 09:27:26.293: E/AndroidRuntime(589): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.SharedPreferences.Login/com.SharedPreferences.Login.SharedPrefLoginActivity}: java.lang.NullPointerException
09-14 09:27:26.293: E/AndroidRuntime(589): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
09-14 09:27:26.293: E/AndroidRuntime(589): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
09-14 09:27:26.293: E/AndroidRuntime(589): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
09-14 09:27:26.293: E/AndroidRuntime(589): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
09-14 09:27:26.293: E/AndroidRuntime(589): at android.os.Handler.dispatchMessage(Handler.java:99)
09-14 09:27:26.293: E/AndroidRuntime(589): at android.os.Looper.loop(Looper.java:123)
09-14 09:27:26.293: E/AndroidRuntime(589): at android.app.ActivityThread.main(ActivityThread.java:4363)
09-14 09:27:26.293: E/AndroidRuntime(589): at java.lang.reflect.Method.invokeNative(Native Method)
09-14 09:27:26.293: E/AndroidRuntime(589): at java.lang.reflect.Method.invoke(Method.java:521)
09-14 09:27:26.293: E/AndroidRuntime(589): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-14 09:27:26.293: E/AndroidRuntime(589): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-14 09:27:26.293: E/AndroidRuntime(589): at dalvik.system.NativeStart.main(Native Method)
09-14 09:27:26.293: E/AndroidRuntime(589): Caused by: java.lang.NullPointerException
09-14 09:27:26.293: E/AndroidRuntime(589): at com.SharedPreferences.Login.SharedPrefLoginActivity.onCreate(SharedPrefLoginActivity.java:58)
09-14 09:27:26.293: E/AndroidRuntime(589): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-14 09:27:26.293: E/AndroidRuntime(589): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)