Android Facebook Login Cannot call LoginFragment with a null calling package

Viewed 591

I have added facebook login to my app.

First time I am able to login without any issue. But after login and then I logout. I am getting this error in the logcat.

E/LoginFragment: Cannot call LoginFragment with a null calling package. This can occur if the launchMode of the caller is singleInstance.

I searched google a bit. Some errors were resolved by setting launchmode to standard. I didn't specify any launchmode. But still I tried with

<activity
     android:name=".activities.LoginActivity"
     android:launchMode="standard"/>

But got no success.

Here is my procedure to login.

1 Added FB dependency compile 'com.facebook.android:facebook-android-sdk:[4,5)'

2 I initialize FB sdk in Application Class's onCreate() After super.onCreate(). AppEventsLogger.activateApp(this);

3 In my login activity created CallbackManager instance on the onCreate() of activity.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    callbackManager = CallbackManager.Factory.create();
    setContentView(R.layout.activity_login);
}

4 I used a custom button to login. When I clicked on fb login button. I am using this code.

LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {

                    GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
                            new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(JSONObject object, GraphResponse response) {
                                    //My Stuff Here
                                }
                            });
                    Bundle parameters = new Bundle();

                    parameters.putString("fields", "id,name,email,gender,first_name,last_name,link,picture.type(large)");
                    request.setParameters(parameters);
                    request.executeAsync();
                }

                @Override
                public void onCancel() {
                    //Cancel Stuff
                }

                @Override
                public void onError(FacebookException exception) {
                    exception.printStackTrace();
                    //Error Stuff
                }
            });        LoginManager.getInstance().logInWithReadPermissions((AppCompatActivity)context,
            Arrays.asList("public_profile, email"));

5 I registered for callbacks in onActivityResult()

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

6 And to logout I have used LoginManager.getInstance().logOut()

7 But I try to login again with my custom button, the debug pointer goes to the line LoginManager.getInstance().registerCallback() but none of the methods (onSuccess(LoginResult loginResult), onError(), onError(FacebookException exception)) of the callback gets call. And I get a log saying E/LoginFragment: Cannot call LoginFragment with a null calling package. This can occur if the launchMode of the caller is singleInstance.

2 Answers

Change this

LoginManager.getInstance().logInWithReadPermissions((AppCompatActivity)context, Arrays.asList("public_profile, email"));

to LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this, Arrays.asList("public_profile, email"));

First of all, Facebook is not giving exact exception details here.

Answer of Sushobh Nadiger helped me get to the issue.

I wanted to isolate the facebook login code from my LoginActivity and return the JSONObject back to LoginActivity so I created a class for handling Social Login called SocialLogin.java and I registered the callback in SocialLogin.java via LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() { //More Stuff here } and called login for facebook LoginManager.getInstance().logInWithReadPermissions() for more details see the full code in question.

This was the actual issue. So I made these calls (registerCallback() and logInWithReadPermissions()) in my LoginActivity.java and it worked.

However I as developer would think that a company like facebook should give us proper error rather than like this misleading error in my case so that our precious time could not be wasted.

Related