Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 371
datasmurfen Avatar asked Feb 11 '23 00:02

datasmurfen


2 Answers

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);
}
like image 171
Bill Tsagkas Avatar answered Feb 13 '23 05:02

Bill Tsagkas


First of all, insert private private CallbackManager callbackManager; into your code. It should look something like this:

callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);

Then add FacebookSdk.sdkInitialize(getApplicationContext());

before super.onCreate(savedInstanceState);

Try this Hope this works

like image 36
Hardy Avatar answered Feb 13 '23 07:02

Hardy