{"record":{"id":"d2752051d7372dbe","repo":"yuliskov/SmartTube","slug":"can-t-create-searchtagsfragment-the-context-is-nu","errorCode":null,"errorMessage":"Can't create SearchTagsFragment: the context is null","messagePattern":"Can't create SearchTagsFragment: the context is null","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"smarttubetv/src/main/java/com/liskovsoft/smartyoutubetv2/tv/ui/search/tags/SearchTagsFragment.java","lineNumber":53,"sourceCode":"\r\npublic class SearchTagsFragment extends SearchTagsFragmentBase {\r\n    private static final String TAG = SearchTagsFragment.class.getSimpleName();\r\n    private SearchPresenter mSearchPresenter;\r\n    private Map<Integer, VideoGroupObjectAdapter> mSearchGroupAdapters;\r\n    private String mSearchQuery;\r\n    private String mNewQuery;\r\n    private VideoCardPresenter mCardPresenter;\r\n    private ShortsCardPresenter mShortsPresenter;\r\n    private SearchData mSearchData;\r\n    private boolean mIsFragmentCreated;\r\n    private CrashRestorer mCrashRestorer;\r\n\r\n    @Override\r\n    public void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(null); // Real restore takes place in the presenter\r\n\r\n        if (getContext() == null) {\r\n            throw new IllegalStateException(\"Can't create SearchTagsFragment: the context is null\");\r\n        }\r\n\r\n        mCrashRestorer = new CrashRestorer(getContext(), savedInstanceState);\r\n        mIsFragmentCreated = true;\r\n        mSearchPresenter = SearchPresenter.instance(getContext());\r\n        mSearchPresenter.setView(this);\r\n        mCardPresenter = new VideoCardPresenter();\r\n        mShortsPresenter = new ShortsCardPresenter();\r\n        mSearchGroupAdapters = new HashMap<>();\r\n        mSearchData = SearchData.instance(getContext());\r\n\r\n        setupEventListeners();\r\n        setKeyboardAutoShowEnabled(mSearchData.isKeyboardAutoShowEnabled());\r\n        setKeyboardFixEnabled(mSearchData.isKeyboardFixEnabled());\r\n        setTypingCorrectionDisabled(mSearchData.isTypingCorrectionDisabled());\r\n    }\r\n\r\n    private void setupEventListeners() {\r","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/yuliskov/SmartTube/blob/3de8d90593e0166f012ca18cc3c7ba45bfd68ac4/smarttubetv/src/main/java/com/liskovsoft/smartyoutubetv2/tv/ui/search/tags/SearchTagsFragment.java#L35-L71","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before — voice-search callback commits regardless of host state\nsearchResultObservable.subscribe(query ->\n        showSearchTags(query)); // host may already be destroyed\n\n// after — check the host first\nsearchResultObservable.subscribe(query -> {\n    if (isAdded() && !requireActivity().isFinishing()) {\n        showSearchTags(query);\n    }\n});","handlingStrategy":"validation","validationCode":"// voice-search / debounce callbacks must check the host before navigating\nif (isAdded() && getContext() != null && !requireActivity().isFinishing()) {\n    showSearchTags(query);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["android","android-tv","fragment","lifecycle","search"],"backgroundTag":"fragment-not-attached","analyzedSha":"3de8d90593e0166f012ca18cc3c7ba45bfd68ac4","analyzedAt":"2026-08-22T09:19:26.383Z","schemaVersion":2},"datasetVersion":"2026-08-22T14:17:55.899Z"}