OnActivityResult上的Facebook Session null

时间:2014-05-19 10:43:15

标签: android facebook session sdk onactivityresult

使用facebook sdk 3.14 for android,当我更改密码然后尝试登录应用程序时,facebook本机应用程序打开并要求我输入新密码。问题是,如果我将我的应用程序放在后台,然后再次恢复我的应用程序并输入新密码并按登录,则应用程序崩溃,因为我的会话在OnActivityResult上变为null。有人可以建议如何避免会话变为null或如何处理它。

// This method gets the user logged in without any permission(basic
// permissions which are default will be asked).
@Override
public void login() {
    session = Session.getActiveSession();

                if (session == null
                || session.getState() == SessionState.CLOSED_LOGIN_FAILED
                || session.getState() == SessionState.CLOSED) {
            session = new Session(context);
        }
        if (!session.isOpened()) {
            session.openForRead(new Session.OpenRequest(this));
        }
        Session.setActiveSession(session);

}


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    session = Session.getActiveSession();
    session.onActivityResult(this.activity, requestCode, resultCode,
                data);
    if (resultCode == Activity.RESULT_OK && session.isOpened()) {
        loginResult(true);
    }

    // If the user cancels the login process or it gets cancelled due to
    // some another reason.
    if (resultCode == Activity.RESULT_CANCELED) {
        Log.e("LOGIN", "CANCELLED");
        inter.loginResult(false);
    }

}



// Facebook Login Result
@Override
public void loginResult(boolean result) {
    // TODO Auto-generated method stub
    if (result) {
        Log.e("Facebook Login ", "Successful");
    } else {
        Log.e("Facebook Logout", "Un Successful");
    }
}

它显示的例外是:

java.lang.RuntimeException: Unable to resume activity   {com.example.FirstDemo/com.example.FirstDemo.FC_LoginActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=199188, result=-1, data=Intent { (has extras) }} to activity {com.example.FirstDemo/com.example.FirstDemo.FC_LoginActivity}: java.lang.NullPointerException

1 个答案:

答案 0 :(得分:0)

试试如下:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    session = Session.getActiveSession();
    if(session !=null)
     {
       session .onActivityResult(this.activity, requestCode, resultCode,
                data);
       if(session.isOpened())
       {
            loginResult(true);
       }
       else
        {
                loginResult(false);
         }
    }
}