如何使用TabGroup启动新活动

时间:2013-01-08 09:49:07

标签: android android-intent

以下代码显示我何时退出,然后另一个活动登录名为。

    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);

}

最后两行代码被注释,因为我已经使用了它们但强制关闭应用程序。以下是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);
}

}

请帮我介绍如何启动登录活动 * 提前致谢 *

1 个答案:

答案 0 :(得分:0)

TabHost tabHost;
TabSpec spec1;
TabSpec spec2;
TabSpec spec3;


        tabHost=getTabHost();

        spec1=tabHost.newTabSpec("Tab 1");
        spec1.setIndicator("Tab !",null);
        Intent in1=new Intent(this,Activity1.class);
        in1.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        spec1.setContent(in1);

        spec2=tabHost.newTabSpec("Tab 2");
        spec2.setIndicator("Tab 2",null);
        Intent in2=new Intent(this,Activity2.class);
        in2.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        in2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        spec2.setContent(in2);

        spec3=tabHost.newTabSpec("Tab3");
        spec3.setIndicator("Tab3",null);
        Intent in3=new Intent(this,Activity3.class);
        in3.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        in3.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        spec3.setContent(in3);

        tabHost.addTab(spec1);
        tabHost.addTab(spec2);
        tabHost.addTab(spec3);

尝试使用Tab活动像这样