我想使用共享首選項創建userlogin詳細信息。使用共享首選項的登錄對話框
當我加載應用程序,並點擊登錄按鈕, 會彈出一個登錄對話框。 我輸入後,它會向用戶敬酒,例如welcome [用戶名]。 當用戶再次登錄時,它會將一條消息歡迎回來[用戶名]。用戶不必再次輸入他/她的名字進入應用程序。 事實上,當用戶點擊登錄時,它會敬酒回來[用戶名],他/她可以繼續申請。
但是,我的代碼中有 ,只要我選擇登錄,我必須輸入用戶名。 即使我再次登錄,我仍然需要輸入用戶名。
有人可以引導我與我的代碼?
的logcat:
01-27 18:04:00.266: E/AndroidRuntime(4287): FATAL EXCEPTION: main
01-27 18:04:00.266: E/AndroidRuntime(4287): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.fuellogproject/com.example.fuellogproject.homeActivity}: java.lang.NullPointerException
01-27 18:04:00.266: E/AndroidRuntime(4287): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
01-27 18:04:00.266: E/AndroidRuntime(4287): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
01-27 18:04:00.266: E/AndroidRuntime(4287): at android.app.ActivityThread.access$700(ActivityThread.java:140)
01-27 18:04:00.266: E/AndroidRuntime(4287): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
01-27 18:04:00.266: E/AndroidRuntime(4287): at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 18:04:00.266: E/AndroidRuntime(4287): at android.os.Looper.loop(Looper.java:137)
01-27 18:04:00.266: E/AndroidRuntime(4287): at android.app.ActivityThread.main(ActivityThread.java:4921)
01-27 18:04:00.266: E/AndroidRuntime(4287): at java.lang.reflect.Method.invokeNative(Native Method)
01-27 18:04:00.266: E/AndroidRuntime(4287): at java.lang.reflect.Method.invoke(Method.java:511)
01-27 18:04:00.266: E/AndroidRuntime(4287): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
01-27 18:04:00.266: E/AndroidRuntime(4287): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
01-27 18:04:00.266: E/AndroidRuntime(4287): at dalvik.system.NativeStart.main(Native Method)
01-27 18:04:00.266: E/AndroidRuntime(4287): Caused by: java.lang.NullPointerException
01-27 18:04:00.266: E/AndroidRuntime(4287): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:160)
01-27 18:04:00.266: E/AndroidRuntime(4287): at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:366)
01-27 18:04:00.266: E/AndroidRuntime(4287): at com.example.fuellogproject.homeActivity.<init>(homeActivity.java:19)
01-27 18:04:00.266: E/AndroidRuntime(4287): at java.lang.Class.newInstanceImpl(Native Method)
01-27 18:04:00.266: E/AndroidRuntime(4287): at java.lang.Class.newInstance(Class.java:1319)
01-27 18:04:00.266: E/AndroidRuntime(4287): at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
01-27 18:04:00.266: E/AndroidRuntime(4287): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2025)
01-27 18:04:00.266: E/AndroidRuntime(4287): ... 11 more
這是我的代碼
public void CheckUser(View v) {
final Dialog dialog = new Dialog(homeActivity.this);
dialog.setContentView(R.layout.login);
dialog.setTitle("Login");
final EditText editTextUserName = (EditText) dialog
.findViewById(R.id.nameEditText);
Button btnLogIn = (Button) dialog.findViewById(R.id.loginBTN);
// get The User name and Password
String userName = editTextUserName.getText().toString();
if (access_token != null) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString("username", userName);
editor.commit();
String getUser = prefs.getString("username", userName);
Toast.makeText(homeActivity.this, "Welcome," + getUser,
Toast.LENGTH_LONG).show();
dialog.dismiss();
Intent mainact = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(mainact);
} else {
Toast.makeText(homeActivity.this, "Welcome back," + userName,
Toast.LENGTH_LONG).show();
Intent mainact = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(mainact);
}
// get the Refferences of views
// Set On ClickListener
btnLogIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString("access_token", "<save value>");
editor.commit();
}
});
dialog.show();
}
什麼是引發NPE的homeActivity.java:19'? –