Facebook处理程序不会被调用

时间:2016-09-28 08:12:37

标签: android facebook-sdk-4.0

从主要活动开始活动:

Intent intent = new Intent(MainActivity.this, FacebookActivity.class);
startActivity(intent);

定义处理程序:

public class FacebookActivity extends Activity {

    LoginButton loginButton;
    CallbackManager callbackManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_facebook);

        loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions(Arrays.asList("public_profile"));
        callbackManager = CallbackManager.Factory.create();

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

但处理程序不会被调用。我在控制台中看到了这一点:

calling onResume for activity

任何想法,有什么问题?在早期版本中,我没有使用单独的活动进行Facebook登录,并且登录处理程序工作正常。奇怪。

3 个答案:

答案 0 :(得分:0)

setContentView

之前调用以下代码
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();

答案 1 :(得分:0)

您必须初始化Facebook-Sdk

在此行之前添加这些行

的setContentView(R.layout.activity_facebook);

FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager  = CallbackManager.Factory.create();

答案 2 :(得分:0)

第1步

在初始化CallbackManager

之前初始化facebook SDK
FacebookSdk.sdkInitialize(getApplicationContext());

第2步

然后在onActivityResult()中将登录结果转发给onCreate()中创建的callbackManager

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