使用Facebook登录Android时无法解析registerCallback

时间:2015-11-30 09:23:12

标签: android facebook

我正在使用this教程为Andrdoid进行Facebook登录。但是,我无法在registerCallback内解决onCreateView

你们有什么想法?在我的LoginActivity.java

中实施onCreateView的地方

2 个答案:

答案 0 :(得分:0)

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Facebook Initialization
    FacebookSdk.sdkInitialize(this.getApplicationContext());
    FacebookSdk.setApplicationId(getString(R.string.facebook_app_id));

    setContentView(R.layout.activity_login);

    initFacebook();
}

 // From Here Facebook Started

private void initFacebook() {
    LoginButton facebookLoginButton = (LoginButton) findViewById(R.id.f_sign_in_button);
    facebookLoginButton.setReadPermissions(Arrays.asList("email"));

    // callback registration
    callbackManager = CallbackManager.Factory.create();

    facebookLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

            Log.d("Login Result", loginResult + "");
            GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject object, GraphResponse response) {
                    Log.d("Result", object.toString() + "" + response.toString() + "");

                    try {
                        fbid = object.getString("id");
                        email = object.getString("email");
                        if (object.getString("first_name") == null) {
                            fName = object.getString("name");
                            lName = "";
                        } else {
                            fName = object.getString("first_name");
                            lName = object.getString("last_name");
                        }

                        authType = Constant.Facebook;
                        image_url = "https://graph.facebook.com/" + fbid + "/picture?type=large";
                        LoginManager.getInstance().logOut();

                        Log.d("Facebook Information", fName + "  " + lName + "  " + email);

                        hitSignInService();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }

            });


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


        }

        @Override
        public void onCancel() {
            // App code
            Log.d("Cancel", "cancel");
        }

        @Override
        public void onError(FacebookException error) {
            // App code
            Log.d("Error", "error");
        }
    });

}

答案 1 :(得分:0)

OnCreate活动方法,你找到登录按钮的id,IN教程他们使用Fragment,所以他们在Oncreateview上做。

OnCreate 活动方法