經過大量調試後,我在過去的8個小時中遇到了這個錯誤,我發現通過Intent
返回的數據在我的onActivityResult
方法中爲null。數據始終爲空onactivityresult facebook android sdk 3.0
這是我用來登錄的代碼。
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方法被調用,數據值爲空的第一次,但下一次,當我點擊它與一些返回的數據相同的按鈕價值觀和我能夠登錄,這並不在某些地方,但在某些設備上
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("");
}
});
}
}
我也有這種方法的麻煩。經過幾十個小時的調試後,很明顯問題是:在Manifest中,持有此代碼的Activity被設置爲'noHistory =「true」'。畢竟,我最終使用了與Fragments一起工作的官方登錄按鈕,因爲上面的方法非常髒,使用內部方法等。 – caw