经过大量调试后,我在过去 8 小时内一直遇到此错误,我发现通过返回的数据在我的方法中Intent
为空。onActivityResult
这是我用来登录的代码。
signInWithFacebook();
在我的活动中通过按钮调用方法,
Session mCurrentSession;
SessionTracker mSessionTracker;
private void signInWithFacebook() {
mSessionTracker = new SessionTracker(getBaseContext(), new StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
}
}, null, false);
String applicationId = Utility.getMetadataApplicationId(getBaseContext());
mCurrentSession = mSessionTracker.getSession();
if (mCurrentSession == null || mCurrentSession.getState().isClosed()) {
mSessionTracker.setSession(null);
Session session = new Session.Builder(getBaseContext()).setApplicationId(applicationId).build();
Session.setActiveSession(session);
mCurrentSession = session;
System.out.println("session closed or null"+mCurrentSession);
}
if (!mCurrentSession.isOpened()) {
Session.OpenRequest openRequest = null;
openRequest = new Session.OpenRequest(loginScreen.this);
if (openRequest != null) {
openRequest.setPermissions(Arrays.asList("user_birthday", "email", "user_location"));
openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
mCurrentSession.openForRead(openRequest);
//mCurrentSession.openForPublish(openRequest);
System.out.println("1111");
}
}else {
Request.executeMeRequestAsync(mCurrentSession, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
System.out.println("..reached here...");
String json=user.getInnerJSONObject().toString();
try {
JSONObject fbJson=new JSONObject(json);
Email=fbJson.getString("email");
Name=fbJson.getString("name");
Id=fbJson.getString("id");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("22222");
if(mProgressDialog1!=null)
mProgressDialog1.dismiss();
DisplayrogressDialog("Signing in ");
new LoginThread(Email, "","facebook",Id).start();
/*Intent mIntent = new Intent(loginScreen.this, SelectCourseActivity.class);
mIntent.putExtra("fbid", Id);
mIntent.putExtra("fbemail", Email);
mIntent.putExtra("fbname", Name);
mIntent.putExtra("signupvia", "facebook");
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(mIntent);*/
}
});
}
}
然后第一次调用 onActivityResult 方法,数据值为 null 但下次当我单击同一个按钮时,它会返回带有一些值的数据并且我能够登录,这不会在某些地方发生,而是在某些设备上发生
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(mProgressDialog1!=null)
mProgressDialog1.setMessage("Connecting to Studycopter...");
else
DisplayrogressDialogFB("Connecting to Studycopter...","Facebook");
System.out.println(" value of getactive session "+Session.getActiveSession().getState()+" data "+ data);
if(Session.getActiveSession().getState().equals("OPENING"))
{
mCurrentSession=Session.openActiveSession(this);
}
Session.getActiveSession().onActivityResult(loginScreen.this, requestCode, resultCode, data);
mCurrentSession=Session.getActiveSession();
if(mCurrentSession==null)
{
mCurrentSession=Session.openActiveSession(this);
}
if (mCurrentSession.isOpened()) {
Request.executeMeRequestAsync(mCurrentSession, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
String json=user.getInnerJSONObject().toString();
try {
JSONObject fbJson=new JSONObject(json);
Email=fbJson.getString("email");
Name=fbJson.getString("name");
Id=fbJson.getString("id");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new FbFurther().execute("");
}
});
}
}