2013-04-18 293 views
2

我正在开发android中的应用程序。我使用SharedPreferences进行会话管理的登录和注销活动。我的应用程序注销,将数据存储在sharedpreferences中以保持会话成功完成。当我从应用程序注销(第二个活动)并获取呈现到第一个活动(登录活动),但是当成功注销后我单击返回在模拟器上的按钮时仍会发生第二个活动,并且遇到类似2.3.3等某些版本例外如何在注销时销毁活动?

java.lang.RuntimeException: Unable to destroy activity {com.oj.bs/com.oj.bs.LoginActivity}: java.lang.NullPointerException 

有人可以解决这个问题。请参考下面的代码

以下是继登录活动代码

sessionmngr = new SessionManager(getApplicationContext()); 

        JSONObject json_user = jsonObj.getJSONObject("user"); 
        sessionmngr.createLoginSession(id,json_user.getString(KEY_UNAME), 
          json_user.getString(KEY_UEMAIL)); 
        . 
        . 

会话管理

public class SessionManager { 

SharedPreferences pref; 
Editor editor; 
Context contxt; 
//shared pref mode 
int PRIVATE_MODE = 0; 
// Sharedpref file name 
private static final String PREF_NAME = "bookingScapePref"; 

// All Shared Preferences Keys 
private static final String IS_LOGIN = "IsLoggedIn"; 
public static final String KEY_NAME = "name"; 
public static final String KEY_ID = "id"; 
public static final String KEY_EMAIL = "email"; 

public SessionManager(Context context) { 
    super(); 
    this.contxt = context; 
    pref = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE); 
    editor = pref.edit(); 
    editor.commit(); 
} 
/** 
* Create login session 
* */ 
public void createLoginSession(int id,String name, String email) { 

    editor.putBoolean(IS_LOGIN,true); 
    editor.putString(KEY_NAME, name); 
    editor.putString(KEY_EMAIL, email); 
    editor.putInt(KEY_ID, id); 
    editor.commit(); 
} 
/** 
* Get stored session data 
* */ 
public HashMap<String, String> getUserDetails() { 
    HashMap<String, String> userDetails = new HashMap<String, String>(); 
    userDetails.put(KEY_NAME, pref.getString(KEY_NAME, null)); 
    userDetails.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null)); 
    return userDetails; 
} 
/** 
* Check login method will check user login status 
* */ 
public void checkLogin() { 

    if (!this.isLoggedIn()) { 
     Intent i = new Intent(contxt, LoginActivity.class); 
     //closing all the activity 
     i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     //Add new Flag to start new Activity 
     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

     contxt.startActivity(i); 
    } 
} 

public void logoutUser() { 
    //clearing all data from sharedPreferences 
    editor.clear(); 
    editor.commit(); 
    Intent intnt = new Intent(contxt, LoginActivity.class); 
    // Closing all the Activities 
    intnt.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    // Add new Flag to start new Activity 
    intnt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

    // Staring Login Activity 
    contxt.startActivity(intnt); 
} 

/** 
* Quick check for login 
* **/ 
// Get Login State 
public boolean isLoggedIn(){ 
    return pref.getBoolean(IS_LOGIN, false); 
} 

}

这是次活动在这里,我正在检查会议

public class ProjectFragActivity extends SherlockFragmentActivity { 

SessionManager sessionMngr ; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    sessionMngr = new SessionManager(getApplicationContext()); 

    Toast.makeText(getApplicationContext(), "User Login Status: " + sessionMngr .isLoggedIn(), 
      Toast.LENGTH_LONG).show(); 
    sessionMngr.checkLogin(); 
      . 
      . 
+0

你可以发布应用程序崩溃的代码(在'onDestroy()') –

+0

也发布你的清单。 –

回答

0
@Override 
public void onBackPressed() { 
// TODO Auto-generated method stub 
    Intent intent = new Intent(Intent.ACTION_MAIN); 
    intent.addCategory(Intent.CATEGORY_HOME); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(intent); 
super.onBackPressed(); 
} 

嗨,使用你的登录屏幕onBackpressed功能和实现这个意图功能。它会带你到手机的主屏幕。

希望这会给你一些解决方案。

0

更简单的解决方案应该是在第二个活动中调用finish();

+0

我完成了关于destroy函数的活动。当我按下后退按钮时,我遇到了类似“java.lang.runtimeException无法销毁活动”的异常。 –

+0

@SwapnilTayade当调用onDestroy()时,活动已经完成。您当时不应该调用'finish()'。发布你的'onDestroy()'的代码 –

0

首先不要拨打finish()destroy(),活动已经完成并离开,再加上一切与其他。你需要做的是在注销时调用finish(),无论你有什么功能登出人员,在最后登录finish()

+0

所以它的工作? – LuckyMe