{"record":{"id":"29e2611a5bdcd305","repo":"Blankj/AndroidUtilCode","slug":"contentview-is-null","errorCode":null,"errorMessage":"ContentView is null.","messagePattern":"ContentView is null\\.","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"lib/base/src/main/java/com/blankj/base/BaseFragment.java","lineNumber":164,"sourceCode":"    @Override\n    public void onSaveInstanceState(@NonNull Bundle outState) {\n        log(\"onSaveInstanceState\");\n        super.onSaveInstanceState(outState);\n        outState.putBoolean(STATE_SAVE_IS_HIDDEN, isHidden());\n    }\n\n    @Override\n    public void onDestroy() {\n        log(\"onDestroy\");\n        super.onDestroy();\n    }\n\n    public void applyDebouncingClickListener(View... views) {\n        ClickUtils.applyGlobalDebouncing(views, mClickListener);\n    }\n\n    public <T extends View> T findViewById(@IdRes int id) {\n        if (mContentView == null) throw new NullPointerException(\"ContentView is null.\");\n        return mContentView.findViewById(id);\n    }\n\n    protected void log(String msg) {\n        if (isDebug == null) {\n            isDebug = AppUtils.isAppDebug();\n        }\n        if (isDebug) {\n            Log.d(\"BaseFragment\", getClass().getSimpleName() + \": \" + msg);\n        }\n    }\n}\n","sourceCodeStart":146,"sourceCodeEnd":177,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/base/src/main/java/com/blankj/base/BaseFragment.java#L146-L177","documentation":"BaseFragment.findViewById throws a NullPointerException when the fragment's content view (mContentView) is null. mContentView is only assigned inside setContentView(), and only when bindLayout() returns a value greater than 0. The library throws hard because findViewById has no meaningful result without an inflated root view.","triggerScenarios":"Calling findViewById(id) before onCreateView/onCreateView has run (e.g., from onCreate, onAttach, or a constructor), or in a BaseFragment subclass whose bindLayout() returns 0 (so setContentView() early-returns and never assigns mContentView).","commonSituations":"Initialising views or wiring listeners in onCreate instead of initView/onViewCreated; overriding bindLayout() to return 0 for a headless/empty fragment then still calling findViewById; calling findViewById from a background thread or a callback that fires after onDestroyView nulls the view.","solutions":["Move any findViewById / view-binding calls out of onCreate and onAttach into initView(savedInstanceState, mContentView) or onViewCreated, where mContentView is guaranteed set.","Ensure your BaseFragment subclass overrides bindLayout() to return a valid @LayoutRes id (> 0) whenever you intend to call findViewById.","If the fragment can legitimately have no layout (bindLayout()==0), guard each call with a null check on mContentView or on getView() before resolving views.","After onDestroyView, null out your view references and stop calling findViewById to avoid acting on a stale/destroyed fragment."],"exampleFix":"// before\n@Override\npublic void onCreate(@Nullable Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    TextView tv = findViewById(R.id.tv); // crashes: mContentView still null\n}\n\n// after\n@Override\npublic void initView(Bundle savedInstanceState, View contentView) {\n    TextView tv = contentView.findViewById(R.id.tv); // safe: contentView is mContentView\n}","handlingStrategy":"validation","validationCode":"// Resolve the root view, not findViewById, and only after it is set.\nView root = getView(); // or mContentView if accessible\nif (root != null) {\n    TextView tv = root.findViewById(R.id.tv);\n}","typeGuard":"// Fragment view-readiness guard\nprivate boolean isViewReady(BaseFragment f) {\n    return f.getView() != null;\n}","tryCatchPattern":null,"preventionTips":["Never call findViewById in onCreate/onAttach; use initView or onViewCreated.","Ensure bindLayout() returns a valid layout id whenever you resolve views.","Null out view fields in onDestroyView and stop reading them afterwards."],"tags":["android","fragment","null-view","lifecycle"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}