Android Studio ExceptionInitializeError Login v2.3 中的 Facebook SDK 4

Facebook SDK 4 in Android Studio ExceptionInitializeError Login v2.3

首先,最重要的是,有人可以向我提供一个示例,该示例仅在登录时连接并切换到另一个片段,这会很棒,这样我将拥有可以查看以理解的工作代码。 当用户连接到 facebook 时,我只是想在测试页面上放置一个登录按钮,然后片段应该改变。我尝试了那里的内容:https://developers.facebook.com/docs/facebook-login/android/v2.3 但我无法使其正常工作并且不真正了解发生了什么......所以我迷路了。我有一个 ExceptionInitializeError 和下面的代码。但我怀疑它是否正确。

我的登录片段:

public View onCreateView(
        LayoutInflater inflater,
        ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.login_frag, container, false);

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

    // Callback registration
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Toast.makeText(getActivity(),"Success",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel() {
            Toast.makeText(getActivity(),"fail",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(FacebookException exception) {
            Toast.makeText(getActivity(),"error",Toast.LENGTH_SHORT).show();
        }
    });
    return view;
}

主要活动:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    monFragmentManager = new MonFragmentManager(getSupportFragmentManager());
    monFragmentManager.showFragment(0, false);
    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
    LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            monFragmentManager.showFragment(1,false);
        }

        @Override
        public void onCancel() {
            monFragmentManager.showFragment(0,false);
        }

        @Override
        public void onError(FacebookException e) {
            monFragmentManager.showFragment(0,false);
        }
    });
}

为了替换片段,您需要在事务实例上获取一个FragmentManager. On this instance, you call .beginTransaction() to obtain a FragmentTransaction instance. On this transaction instance, you can call .replace(). This takes two arguments: The ID of the container inside which you want to replace fragments, and the instance of the fragment that you want to display after the replace. Finally, you need to call .commit()的实例以使更改生效。

你可以在你的 MainActivity:

中的匿名内部 FacebookCallback<LoginResult> class 中定义的 onSuccess() 方法中完成所有这些操作
// ...
// obtain someFragmentManager instance
// obtain containerViewId instance
// ...

@Override
public void onSuccess(LoginResult loginResult) {
  someFragmentManager.beginTransaction()
    .replace(containerViewId, otherFragment)
    .commit();
}

您可以在 this page of the Android docs 上看到替换片段的示例。