Facebook SDK不能在片段中工作

卡斯珀

登录时facebook,我的回调无法正常工作。

我不知道为什么这部分不起作用: loginButton.registerCallback(callbackManager...

这是我的代码:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return initView(inflater, container);
    }

    private View initView(LayoutInflater inflater, ViewGroup container) {

       FacebookSdk.sdkInitialize(getActivity().getApplicationContext());


        callbackManager = CallbackManager.Factory.create();
        Log.d("FB","1");

        final   View view = inflater.inflate(R.layout.fragment_login, container, false);
        name=(TextView)view.findViewById(R.id.FBname);
        link=(TextView)view.findViewById(R.id.FBlink);
        id=(TextView)view.findViewById(R.id.FBid);

        LoginButton loginButton = (LoginButton)view.findViewById(R.id.login_button);

      loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {


            @Override
            public void onSuccess(LoginResult loginResult) {


                accessToken = loginResult.getAccessToken();

                Log.d("FB","access token got.");

                //send request and call graph api

                GraphRequest request = GraphRequest.newMeRequest(
                        accessToken,
                        new GraphRequest.GraphJSONObjectCallback() {



                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {



                                Log.d("FB","complete");
                                Log.d("FB",object.optString("name"));
                                name.setText("name:" + object.optString("name"));
                                Log.d("FB", object.optString("link"));
                                link.setText("link:"+object.optString("link"));
                                Log.d("FB", object.optString("id"));
                                id.setText("id:"+object.optString("id"));

                            }
                        });



                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,link");
                request.setParameters(parameters);
                request.executeAsync();
            }


            @Override
            public void onCancel() {
                // App code

                Log.d("FB", "CANCEL");
            }



            @Override
            public void onError(FacebookException exception) {
                // App code

                Log.d("FB",exception.toString());
            }
        });

        return view;
    }

感谢所有受访者

奥希弗

您是否将此添加到了活动中?

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

            callbackManager.onActivityResult(requestCode, resultCode, data);
        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章