中無法正常工作我正在集成Facebook
登錄我的android應用程序並使用Graph API
訪問用戶信息。圖形API在android
我收到一個不尋常的行爲,當我啓動的應用程序中,我沒有登錄Facebook
,然後嘗試登錄Facebook
,然後它允許我成功登錄,但不允許獲取用戶信息(即JSON
對象上的NullPointerException
),並崩潰。
另一方面,如果我再次重新啓動已登錄的應用程序(在應用程序崩潰之前成功登錄),然後按下注銷按鈕,然後再次登錄按鈕。現在奇怪的事情發生了,我可以在成功登錄後訪問用戶信息。請幫助我在第一種情況下訪問用戶信息。
這裏是我的代碼:
public class Login extends Activity implements View.OnClickListener{
private LoginButton facebookLogin;
private CallbackManager callbackManager;
private AccessTokenTracker accessTokenTracker;
private AccessToken accessToken;
private Profile facebookProfile;
private ProfileTracker profileTracker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
setContentView(R.layout.activity_login);
callbackManager = CallbackManager.Factory.create();
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(
AccessToken oldAccessToken,
AccessToken currentAccessToken) {
// Set the access token using
// currentAccessToken when it's loaded or set.
}
};
// If the access token is available already assign it.
accessToken = AccessToken.getCurrentAccessToken();
facebookProfile = Profile.getCurrentProfile();
/*profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(
Profile oldProfile,
Profile currentProfile) {
// App code
}
};*/
facebookLogin = (LoginButton)findViewById(R.id.button_facebookLogin_Login);
facebookLogin.setReadPermissions(Arrays.asList("public_profile", "email"));
facebookLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
facebookLogin.setReadPermissions(Arrays.asList("public_profile", "email"));
GraphRequest request = GraphRequest.newMeRequest(accessToken,
new GraphRequest.GraphJSONObjectCallback(){
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.d("TOP","Object = "+object.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender");
request.setParameters(parameters);
request.executeAsync();
// App code
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onDestroy() {
super.onDestroy();
//accessTokenTracker.stopTracking();
//profileTracker.stopTracking();
}
}
http://stackoverflow.com/questions/37183258/how-to-get-email-from-facebook-sdk-4-0/37183573#37183573 –