Error calling com.facebook.login.widget.Loginbutton.registerCallback' method on a null Object Reference

Asked

Viewed 24 times

-2

Whenever I start the application this error appears:

Attempt to invoke virtual method 'void com.facebook.login.widget.LoginButton.registerCallback(com.facebook.CallbackManager, com.facebook.FacebookCallback)' on a null object reference.

Follows part of the code:

 login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onSuccess(LoginResult loginResult) {
  • 1

    It’s probably Java on Android, if that’s the case you probably added the code BEFORE setContentView, soon the elements as widgets are not yet ready, but I have no psychic powers, I can not know how this your code at the moment, so I have no help beyond this, the only way to be sure is if you follow the rules of the site and always create a minimal example, Complete and verifiable: https://answall.com/help/minimal-reproducible-example

  • Thank you Guiherme helped me so much !!!!

  • Blz Urilo, I formulated in response, if you think this OK mark it as correct, grateful!

1 answer

1

The error probably indicates that the elements are not ready, in case the widget com.facebook.login.widget.LoginButton who tried to register the callback

On Android the elements will only be available after the setContentView, if trying to record events in widgets before will cause crashes, you must have done something like:

super.onCreate(savedInstanceState);

login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
    @SuppressLint("SetTextI18n")
    @Override
    public void onSuccess(LoginResult loginResult) {
    }
});

...

setContentView(R.layout.activity_main);

And the right thing must be:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
    @SuppressLint("SetTextI18n")
    @Override
    public void onSuccess(LoginResult loginResult) {
       ...
    }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.