yuliskov/SmartTube · error · IllegalStateException

Can't create PlaybackFragment: the context is null

Error message

Can't create PlaybackFragment: the context is null

What it means

PlaybackFragment.onCreate checks getContext() before doing anything else, because every following line needs a Context (BackgroundManager from the host LeanbackActivity, ExoPlayerInitializer, PlaybackPresenter). A null context at onCreate means the fragment has no attached host, so it throws IllegalStateException to fail fast with a clear message instead of an NPU later. Note super.onCreate(null) is passed deliberately (preset-restore workaround), so the null check is the fragment's own guard.

Source

Thrown at smarttubetv/src/main/java/com/liskovsoft/smartyoutubetv2/tv/ui/playback/PlaybackFragment.java:134

    private DebugInfoManager mDebugInfoManager;
    private UriBackgroundManager mBackgroundManager;
    private RowsSupportFragment mRowsSupportFragment;
    private boolean mIsUIAnimationsEnabled = false;
    private boolean mIsEngineBlocked;
    private MediaSessionCompat mMediaSession;
    private MediaSessionConnector mMediaSessionConnector;
    private DoubleTapPlayerAdapter mDoubleTapPlayerAdapter;
    private YouTubeOverlay mYouTubeOverlay;
    private Boolean mIsControlsShownPreviously;
    private Video mPendingFocus;
    private String mSelectedVideoId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(null); // trying to fix bug with presets

        if (getContext() == null) {
            throw new IllegalStateException("Can't create PlaybackFragment: the context is null");
        }

        mSelectedVideoId = savedInstanceState != null ? savedInstanceState.getString(SELECTED_VIDEO_ID, null) : null;
        mVideoGroupAdapters = new HashMap<>();
        mBackgroundManager = getLeanbackActivity().getBackgroundManager();
        mBackgroundManager.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.player_background));
        mPlayerInitializer = new ExoPlayerInitializer(getContext());

        mPlaybackPresenter = PlaybackPresenter.instance(getContext());
        mPlaybackPresenter.setView(this);
        mExoPlayerController = new ExoPlayerController(getContext(), mPlaybackPresenter);

        // Fix open previous video
        if (mPlaybackPresenter.getVideo() != null) {
            mSelectedVideoId = null;
        }

        initPresenters();

View on GitHub (pinned to 3de8d90593)

Solutions

  1. Open PlaybackFragment only through FragmentManager/leanback playback transactions from a live host activity.
  2. Before starting playback from async callbacks, verify the host exists and is not finishing/destroyed (isAdded(), !requireActivity().isFinishing()).
  3. Cancel pending navigation (handlers/rx subscriptions) when the host is destroyed, or guard them with isAdded().
  4. In tests, host the fragment with FragmentScenario attached to a real LeanbackActivity-derived activity.

Example fix

// before — navigation from a detached async callback
mHandler.postDelayed(() -> showPlaybackFragment(video), 5000);
// activity may be gone when this fires → getContext() == null at onCreate

// after — guard with attach state
mHandler.postDelayed(() -> {
    if (isAdded() && !requireActivity().isFinishing()) {
        showPlaybackFragment(video);
    }
}, 5000);
Defensive patterns

Strategy: validation

Validate before calling

// before starting playback from any async path
if (isAdded() && getContext() != null && !requireActivity().isFinishing()) {
    startPlaybackFragment(video);
}

Prevention

When it happens

Trigger: PlaybackFragment instantiated or restored without a host: manual construction with direct lifecycle calls, restore after the activity was destroyed, or navigation triggered from a detached/async callback where the host is gone by the time onCreate runs.

Common situations: Process-death or configuration-change restore races; deep-link handling that shows playback after the activity finished; background events (e.g., player notifications, auto-play timers) starting playback onto a dead host; unit tests creating the fragment directly.

Related errors


AI-assisted analysis of yuliskov/SmartTube@3de8d90593 (2026-08-22). Data as JSON: /api/errors/e48b1d7e9057d01d. Report an issue: GitHub.