在Facebook登录结果期间永远不会返回结果

时间:2015-06-12 22:52:52

标签: android facebook-login facebook-android-sdk

我使用Facebook Login的简单应用程序遇到了一些问题。我已经检查了官方教程和其他一些材料,但我仍然无法得到结果。问题是调用onActivityResult方法,但不调用回调方法。对我来说一切都很好看,所以我想我必须要错过一些东西。这是我的片段代码:

public class MainActivityFragment extends Fragment {

    private CallbackManager callbackManager;

    public MainActivityFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        final LoginButton loginButton = (LoginButton)view.findViewById(R.id.login_button);
        loginButton.setReadPermissions("public_profile");
        loginButton.setFragment(this);
        loginButton.registerCallback(callbackManager, facebookCallback);
        return view;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(resultCode, requestCode, data);
    }

    private FacebookCallback<LoginResult> facebookCallback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            Profile profile = Profile.getCurrentProfile();
            if(profile != null) {
                Log.d("TAG","Welcome: " + profile.getName());
            } else {
                Log.d("TAG","Null?");
            }
        }

        @Override
        public void onCancel() {
            Log.d("TAG", "Cancel");
        }

        @Override
        public void onError(FacebookException e) {
            Log.d("TAG", "Error: " + e);
        }
    };
}

在我的主要活动中,我只有Android Studio生成的代码,并且在Manifest中我拥有Facebook SDK所需的一切。散列码和包名称看起来是正确的。

1 个答案:

答案 0 :(得分:1)

这是你的问题:

callbackManager.onActivityResult(resultCode, requestCode, data);

应该是:

callbackManager.onActivityResult(requestCode, resultCode, data);