yuliskov/SmartTube · error · IllegalStateException
Can't create SearchTagsFragment: the context is null
Error message
Can't create SearchTagsFragment: the context is null
What it means
SearchTagsFragment.onCreate immediately requires getContext() != null because the next lines construct CrashRestorer, SearchPresenter and SearchData with that context. No host means IllegalStateException('Can't create SearchTagsFragment: the context is null') instead of NPEs inside the presenters. Like PlaybackFragment, super.onCreate(null) is intentional (the real state restore happens in the presenter).
Source
Thrown at smarttubetv/src/main/java/com/liskovsoft/smartyoutubetv2/tv/ui/search/tags/SearchTagsFragment.java:53
public class SearchTagsFragment extends SearchTagsFragmentBase {
private static final String TAG = SearchTagsFragment.class.getSimpleName();
private SearchPresenter mSearchPresenter;
private Map<Integer, VideoGroupObjectAdapter> mSearchGroupAdapters;
private String mSearchQuery;
private String mNewQuery;
private VideoCardPresenter mCardPresenter;
private ShortsCardPresenter mShortsPresenter;
private SearchData mSearchData;
private boolean mIsFragmentCreated;
private CrashRestorer mCrashRestorer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(null); // Real restore takes place in the presenter
if (getContext() == null) {
throw new IllegalStateException("Can't create SearchTagsFragment: the context is null");
}
mCrashRestorer = new CrashRestorer(getContext(), savedInstanceState);
mIsFragmentCreated = true;
mSearchPresenter = SearchPresenter.instance(getContext());
mSearchPresenter.setView(this);
mCardPresenter = new VideoCardPresenter();
mShortsPresenter = new ShortsCardPresenter();
mSearchGroupAdapters = new HashMap<>();
mSearchData = SearchData.instance(getContext());
setupEventListeners();
setKeyboardAutoShowEnabled(mSearchData.isKeyboardAutoShowEnabled());
setKeyboardFixEnabled(mSearchData.isKeyboardFixEnabled());
setTypingCorrectionDisabled(mSearchData.isTypingCorrectionDisabled());
}
private void setupEventListeners() {
View on GitHub (pinned to 3de8d90593)
Solutions
- Open SearchTagsFragment only through a FragmentManager transaction from a live host.
- Guard async search-result navigation with isAdded()/!isFinishing() before committing the transaction.
- Cancel or de-register pending search callbacks (rx disposables, handlers) when the host is destroyed.
- In tests, attach the fragment with FragmentScenario to a real host activity.
Example fix
// before — voice-search callback commits regardless of host state
searchResultObservable.subscribe(query ->
showSearchTags(query)); // host may already be destroyed
// after — check the host first
searchResultObservable.subscribe(query -> {
if (isAdded() && !requireActivity().isFinishing()) {
showSearchTags(query);
}
}); Defensive patterns
Strategy: validation
Validate before calling
// voice-search / debounce callbacks must check the host before navigating
if (isAdded() && getContext() != null && !requireActivity().isFinishing()) {
showSearchTags(query);
} Prevention
- Attach SearchTagsFragment via FragmentManager transactions only.
- Dispose rx subscriptions and remove handler callbacks when the host is destroyed.
- Treat delayed search navigation like playback navigation: always check isAdded() first.
When it happens
Trigger: The fragment being created without an attached host: manual instantiation with direct lifecycle calls, restore after host teardown, or search navigation fired from an async callback (voice search result, debounce timer) that lands after the activity is gone.
Common situations: Voice-search results arriving after the search activity finished; process-death restore of the search screen; retained fragment instances reused across hosts; tests instantiating the fragment directly.
Related errors
- VideoGridFragment doesn't attached to the activity
- Can't create PlaybackFragment: the context is null
- Unknown Uri: ${uri}
- Can't create root of SurfacePlaybackFragment
- You must pass either Activity, Fragment or SupportFragment
AI-assisted analysis of yuliskov/SmartTube@3de8d90593 (2026-08-22).
Data as JSON: /api/errors/d2752051d7372dbe.
Report an issue: GitHub.