NullPointer使用Facebook SDK

时间:2016-05-17 11:33:48

标签: android facebook

我在我的应用中使用Facebook SKD,我有下一个错误:

callBackmanager方法中的onActivityResult()为空,我得到NullPointerException

这是我的代码:

parent.findViewById(R.id.fb_login_button).setOnClickListener(this);

其中 R.id.fb_login_button - 我的fb。

自定义按钮

当我点击此按钮时,我的听众会调用此方法:

private void onFacebookButtonClick() {

    initFacebook(getView());
    mFacebookLoginButton.performClick(); // this is button from SDK, Visibility - GONE
}

这个initFacebook(View parent)方法:

callbackManager = CallbackManager.Factory.create();

mFacebookLoginButton = (LoginButton) parent.findViewById(R.id.login_button);
mFacebookLoginButton.setReadPermissions("public_profile");
mFacebookLoginButton.setFragment(this);
mFacebookLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {

        String userId = loginResult.getAccessToken().getUserId();
        trySocialLogin(userId, Constants.PROVIDER_FACEBOOK);
    }

    @Override
    public void onCancel() {
        Log.e("FB", "onCancel()");
    }

    @Override
    public void onError(FacebookException exception) {
        exception.printStackTrace();
    }
});

此代码来自onActivityResult()

callbackManager.onActivityResult(requestCode, resultCode, data); // callbackManager is null here - why?

添加logcat:

    Fatal Exception: java.lang.RuntimeException: Unable to resume activity {com.nashkvartal/com.nashkvartal.StartScreenActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=129742, result=0, data=null} to activity {com.nashkvartal/com.nashkvartal.StartScreenActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'boolean com.facebook.CallbackManager.onActivityResult(int, int, android.content.Intent)' on a null object reference
       at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3287)
       at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3318)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2661)
       at android.app.ActivityThread.access$800(ActivityThread.java:178)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)
       at android.os.Handler.dispatchMessage(Handler.java:111)
       at android.os.Looper.loop(Looper.java:194)
       at android.app.ActivityThread.main(ActivityThread.java:5691)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=129742, result=0, data=null} to activity {com.nashkvartal/com.nashkvartal.StartScreenActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'boolean com.facebook.CallbackManager.onActivityResult(int, int, android.content.Intent)' on a null object reference
       at android.app.ActivityThread.deliverResults(ActivityThread.java:3887)
       at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3269)
       at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3318)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2661)
       at android.app.ActivityThread.access$800(ActivityThread.java:178)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)
       at android.os.Handler.dispatchMessage(Handler.java:111)
       at android.os.Looper.loop(Looper.java:194)
       at android.app.ActivityThread.main(ActivityThread.java:5691)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by java.lang.NullPointerException: Attempt to invoke interface method 'boolean com.facebook.CallbackManager.onActivityResult(int, int, android.content.Intent)' on a null object reference
       at com.nashkvartal.fragments.LoginFragment.onActivityResult(LoginFragment.java:105)
       at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:176)
       at com.nashkvartal.StartScreenActivity.onActivityResult(StartScreenActivity.java:74)
       at android.app.Activity.dispatchActivityResult(Activity.java:6344)
       at android.app.ActivityThread.deliverResults(ActivityThread.java:3883)
       at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3269)
       at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3318)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2661)
       at android.app.ActivityThread.access$800(ActivityThread.java:178)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)
       at android.os.Handler.dispatchMessage(Handler.java:111)
       at android.os.Looper.loop(Looper.java:194)
       at android.app.ActivityThread.main(ActivityThread.java:5691)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

UPD - 添加了onActivityResult()完整代码

@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {

    if (currentProvider == Constants.PROVIDER_FACEBOOK) {           
        callbackManager.onActivityResult(requestCode, resultCode, data); // I catch NullPointer here
    } 

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

1 个答案:

答案 0 :(得分:2)

  

根据较新的Facebook SDK。您应该override onActivityResultsuper

对于较新的SDK使用:

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

并确保要使登录正常工作,您的onActivityResult()方法应该在FacebookCallback匿名类之外。

编辑1

  

从代码开始,您需要在setContentView之前初始化SDK。目前在您的代码中,它在setContentView下面调用,因此请更改它。

喜欢这样。

FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_start_screen);