次のコードは、ログアウトしたときに別のアクティビティ ログインが呼び出されたことを示しています。
public void logoutUser(){
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
// After logout redirect user to Loing Activity
Intent i = new Intent(_context, Login.class);
TabGroupActivity parentActivity = (TabGroupActivity) _context;
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//_context.startActivity(i);
// parentActivity.startChildActivity("Login", i);
}
コードの最後の 2 行は、両方を使用したためコメントされていますが、強制的にアプリを閉じます。以下は、logcat の出力です。
01-08 15:13:56.219: D/AndroidRuntime(877): Shutting down VM
01-08 15:13:56.219: W/dalvikvm(877): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-08 15:13:56.249: E/AndroidRuntime(877): FATAL EXCEPTION: main
01-08 15:13:56.249: E/AndroidRuntime(877): java.lang.NullPointerException
01-08 15:13:56.249: E/AndroidRuntime(877): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
01-08 15:13:56.249: E/AndroidRuntime(877): at com.fabletechnologies.utils.SessionManagement.logoutUser(SessionManagement.java:102)
01-08 15:13:56.249: E/AndroidRuntime(877): at com.fabletechnologies.smartaway.DashboardActivity$1.onClick(DashboardActivity.java:54)
01-08 15:13:56.249: E/AndroidRuntime(877): at android.view.View.performClick(View.java:2408)
01-08 15:13:56.249: E/AndroidRuntime(877): at android.view.View$PerformClick.run(View.java:8816)
01-08 15:13:56.249: E/AndroidRuntime(877): at android.os.Handler.handleCallback(Handler.java:587)
01-08 15:13:56.249: E/AndroidRuntime(877): at android.os.Handler.dispatchMessage(Handler.java:92)
01-08 15:13:56.249: E/AndroidRuntime(877): at android.os.Looper.loop(Looper.java:123)
1-08 15:13:56.249: E/AndroidRuntime(877): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-08 15:13:56.249: E/AndroidRuntime(877): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 15:13:56.249: E/AndroidRuntime(877): at java.lang.reflect.Method.invoke(Method.java:521)
01-08 15:13:56.249: E/AndroidRuntime(877): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-08 15:13:56.249: E/AndroidRuntime(877): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-08 15:13:56.249: E/AndroidRuntime(877): at dalvik.system.NativeStart.main(Native Method)
完全なコードはこちらです。
public class SessionManagement extends Activity{
SharedPreferences pref;
Editor editor;
Context _context;
int PRIVATE_MODE=0;
// Sharedpref file name
private static final String PREF_NAME = "LoginLogoutPref";
// All Shared Preferences Keys
private static final String IS_LOGIN = "IsLoggedIn";
// User name (make variable public to access from outside)
public static final String KEY_NAME = "fullname";
// Email address (make variable public to access from outside)
public static final String KEY_EMAIL = "email";
// Constructor
public SessionManagement(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
/**
* Create login session
* */
public void createLoginSession(String fullname, String email){
// Storing login value as TRUE
editor.putBoolean(IS_LOGIN, true);
// Storing name in pref
editor.putString(KEY_NAME, fullname);
// Storing email in pref
editor.putString(KEY_EMAIL, email);
// commit changes
editor.commit();
}
/**
* Check login method wil check user login status
* If false it will redirect user to login page
* Else won't do anything
* */
public void checkLogin(){
// Check login status
if(!this.isLoggedIn()){
// user is not logged in redirect him to Login Activity
Intent i = new Intent(_context, Login.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
}
/**
* Get stored session data
* */
public HashMap<String, String> getUserDetails(){
HashMap<String, String> user = new HashMap<String, String>();
// user name
user.put(KEY_NAME, pref.getString(KEY_NAME, null));
// user email id
user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
// return user
return user;
}
/**
* Clear session details
* */
public void logoutUser(){
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
// After logout redirect user to Loing Activity
Intent i = new Intent(_context, Login.class);
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
// i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//_context.startActivity(i);
parentActivity.startChildActivity("Login", i);
}
/**
* Quick check for login
* **/
// Get Login State
public boolean isLoggedIn(){
return pref.getBoolean(IS_LOGIN, false);
}
}
ログイン アクティビティの開始方法を教えてください *よろしくお願いします*