pockethub/PocketHub · error · IllegalArgumentException

Activity cannot be null

Error message

Activity cannot be null

What it means

AccountUtils.getAccount() requires a non-null Activity to resolve accounts and show account selection UI. It defensively validates its argument and throws IllegalArgumentException when the caller passes null. This is a programmer-error guard, not a runtime condition.

Solutions

  1. Ensure a valid, attached Activity is available before calling getAccount — check isAdded() in a Fragment.
  2. Pass requireActivity() (Fragment) instead of getContext() when an Activity is required.
  3. Do not cache the Activity in a long-lived field; re-fetch it at call time.
  4. If the call can happen post-detach, skip it instead of calling with null.

Example fix

// before
Account account = AccountUtils.getAccount(getContext(), account, ...);
// after
if (getActivity() != null && isAdded()) {
    Account account = AccountUtils.getAccount(getActivity(), account, ...);
}
Defensive patterns

Strategy: validation

Validate before calling

if (activity == null || activity.isFinishing()) {
    return; // skip account lookup
}

Type guard

fun Fragment.activityOrNull(): Activity? = if (isAdded) activity else null

Prevention

When it happens

Trigger: Calling AccountUtils.getAccount(context-as-Activity, ...) with a null Activity — typically passing getContext() from a Fragment before it is attached, or storing an Activity reference after onDestroy().

Common situations: See trigger scenarios.


AI-assisted analysis of pockethub/PocketHub@8228cb8f71 (2026-09-11). Data as JSON: /api/errors/a17b5c2cc9e06c33. Report an issue: GitHub.

Appendix: source

Thrown at app/src/main/java/com/github/pockethub/android/accounts/AccountUtils.java:232

    /**
     * Get account used for authentication
     *
     * @param manager
     * @param activity
     * @return account
     * @throws IOException
     * @throws AccountsException
     */
    public static Account getAccount(final AccountManager manager,
            final Activity activity) throws IOException, AccountsException {
        final boolean loggable = Log.isLoggable(TAG, DEBUG);
        if (loggable) {
            Log.d(TAG, "Getting account");
        }

        if (activity == null) {
            throw new IllegalArgumentException("Activity cannot be null");
        }

        if (activity.isFinishing()) {
            throw new OperationCanceledException();
        }

        Account[] accounts;
        try {
            if (!hasAuthenticator(manager)) {
                throw new AuthenticatorConflictException();
            }

            while ((accounts = getAccounts(manager)).length == 0) {
                if (loggable) {
                    Log.d(TAG, "No GitHub accounts for activity=" + activity);
                }

                Bundle result = manager.addAccount(ACCOUNT_TYPE, null, null,

View on GitHub (pinned to 8228cb8f71)