Android Facebook api 3.0 error: Cannot call LoginActivity with a null calling package

I managed to find my problem. Although I wasn't setting

android:launchMode="singleTask"

my LoginActivity had

android:noHistory="true"

which results in that exception. I put noHistory true because I didn't want the user to be able to press back button on the first activity after login and go back to login screen. Now I need to find another solution.


After lots of searching I figured out that there don't seems to be a way to startActivityForResult with LocalActivityManager used in the tabs.

So I ended up accepting that it will need an activity filling the entire screen. The activity is only shown a second or so with good network connection - I have made it with a republish option on errors also..

Start publish activity:

Intent intent = new Intent(this, FacebookShareActivity.class);
intent.putExtra(Constants.FACEBOOK_MESSAGE, shareMessage.getMessage());
startActivityForResult(intent, 1);

Facebook share activity code - publishing to users wall:

public class FacebookShareActivity extends Activity {
    String message;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.facebook_publishing);

        message = getIntent().getExtras().getString(Constants.FACEBOOK_MESSAGE);
        createFacebookConnection();
    }

    public void republishButton_Click(View view){
        setVisibilityForRepublishButton(false);
        createFacebookConnection();
    }

    public void createFacebookConnection() {
        Session session = new Session(this);
        Session.setActiveSession(session);

        Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

        Session.StatusCallback statusCallback = new Session.StatusCallback() {
            @Override
            public void call(Session session, SessionState state, Exception exception) {
                String message = "Facebook session status changed - " + session.getState() + " - Exception: " + exception;
                //Toast.makeText(FacebookShareActivity.this, message, Toast.LENGTH_SHORT).show();
                Log.w("Facebook test", message);

                if (session.isOpened() || session.getPermissions().contains("publish_actions")) {
                    publishToWall();
                } else if (session.isOpened()) {
                    OpenRequest open = new OpenRequest(FacebookShareActivity.this).setCallback(this);
                    List<String> permission = new ArrayList<String>();
                    permission.add("publish_actions");
                    open.setPermissions(permission);
                    Log.w("Facebook test", "Open for publish");
                    session.openForPublish(open);
                }
            }
        };

        if (!session.isOpened() && !session.isClosed() && session.getState() != SessionState.OPENING) {
            session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
        } else {
            Log.w("Facebook test", "Open active session");
            Session.openActiveSession(this, true, statusCallback);
        }
    }

    private void setVisibilityForRepublishButton(Boolean visible) {
        ((Button) findViewById(R.id.republishButton)).setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
        //Toast.makeText(FacebookShareActivity.this, "onActivityResult", Toast.LENGTH_SHORT).show();
    }

    void publishToWall() {
        Session session = Session.getActiveSession();

        Bundle postParams = new Bundle();
        postParams.putString("message", message);

        final Context context = this;
        Request.Callback callback = new Request.Callback() {
            public void onCompleted(Response response) {
                FacebookRequestError error = response.getError();
                if (error != null) {
                    setVisibilityForRepublishButton(true);
                    Toast.makeText(context, error.getErrorMessage(), Toast.LENGTH_SHORT).show();
                } else {
                    JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
                    String postId = null;
                    try {
                        postId = graphResponse.getString("id");
                    } catch (JSONException e) {
                        setVisibilityForRepublishButton(true);
                        Log.i("Facebook error", "JSON error " + e.getMessage());
                    }
                    //Toast.makeText(context, postId, Toast.LENGTH_LONG).show();
                    finish();
                }
            }
        };

        Request request = new Request(Session.getActiveSession(), "me/feed", postParams, HttpMethod.POST, callback);

        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();
    }
}

I've got the same problem : trying to log into facebook, with the dialog provided inside the SDK, but from an activity that was itself inside a tabgroup ; like ShareActivity above.

What I've done is basically called startActivityForResult on parent activity of ShareActivity (that is ShareGroupActivity), instead of calling it on ShareActivity.

So 1 , add this in ShareGroupActivity :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    System.out.println("facebook status called");
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}

2 you need to modify the class Session, inside FacebookSDK project, under src com.facebook

2.1 add a boolean member

public boolean insideTabGroup;

2.2 modify StartActivityDelegate, that is used by session to open login ; add the boolean as parameter

interface StartActivityDelegate {
    public void startActivityForResult(Intent intent, int requestCode, boolean insideTabGroup);

    public Activity getActivityContext();
}

2.3 inside the inner class AuthorizationRequest, modify the implementation of this delegate :

    AuthorizationRequest(final Activity activity) {
        startActivityDelegate = new StartActivityDelegate() {
            @Override
            public void startActivityForResult(Intent intent, int requestCode, boolean insideTabGroup) {
                if(insideTabGroup) {
                    ActivityGroup parentActivity = (ActivityGroup) activity.getParent();
                    parentActivity.startActivityForResult(intent,requestCode);

                } else {
                    activity.startActivityForResult(intent, requestCode);
                }
            }

            @Override
            public Activity getActivityContext() {
                return activity;
            }
        };
    }

2.4 Also, modify the other constructors of AuthorizationRequest, by just adding the boolean parameter. As I do not use login to facebook from somewhere else than an activity, that's ok.

2.5 Modifiy the tryLoginActivity method of Session class, to use the boolean member as a parameter :

private boolean tryLoginActivity(AuthorizationRequest request) {
    Intent intent = getLoginActivityIntent(request);

    if (!resolveIntent(intent)) {
        return false;
    }

    try {
        request.getStartActivityDelegate().startActivityForResult(intent, request.getRequestCode(),this.insideTabGroup);

    } catch (ActivityNotFoundException e) {
        return false;
    }

    return true;
}

3 Set the boolean member in the session :

Session session = Session.getActiveSession();
session.insideTabGroup = true;

That should do the trick.

Cdt