datasmurfen datasmurfen - 4 years ago 1179
Java Question

Login button facebook android doesn't redirect to new activity

When i run my Android app, and click approve to the give permissions it not get redirected to the MainActivity. The "Logged in" message doesn't shows up in the Catlog. I have read the Facebook developers guide, and compared my code to different topics here at Stack. I can't see i have done anything wrong.

I would be very glad for help.

public class Login extends Activity {

/**
* Called when the activity is first created.
*/

private CallbackManager callbackManager;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_login);
final CallbackManager callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("public_profile", "email", "user_friends");


loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {


@Override
public void onSuccess(LoginResult loginResult) {

Intent i = new Intent(Login.this, MainActivity.class);
startActivity(i);
System.out.print("Logged in");

}

@Override
public void onCancel() {
// App code

}

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


});
}

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

}


StackTrace

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=64206, result=-1, data=Intent { (has extras) }} to activity {org.MYapp.FBtestApp/org.MYapp.FBtestApp.Login}: 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:3974)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4017)

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 org.MYapp.FBtestApp.Login.onActivityResult(Login.java:98)

Answer Source

For the login to work, you have to remove the onActivityResult() method from inside the FacebookCallback anomymous class. The correct code should look like this:

private CallbackManager callbackManager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    FacebookSdk.sdkInitialize(getApplicationContext());

    callbackManager = CallbackManager.Factory.create();

    LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
    loginButton.setReadPermissions("public_profile", "email", "user_friends");

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Intent i = new Intent(Login.this, MainActivity.class);
            startActivity(i);
            System.out.print("Logged in");
        }

        @Override
        public void onCancel() {
            // App code

        }

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

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data); 
    callbackManager.onActivityResult(requestCode, resultCode, data);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download