{"record":{"id":"6e6a146952d51912","repo":"Tencent/QMUI_Android","slug":"must-use-the-instance-of-qmuiwebviewclient","errorCode":null,"errorMessage":"must use the instance of QMUIWebViewClient","messagePattern":"must use the instance of QMUIWebViewClient","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"qmui/src/main/java/com/qmuiteam/qmui/widget/webview/QMUIWebView.java","lineNumber":130,"sourceCode":"        mOnScrollChangeListeners.remove(listener);\n    }\n\n    public void removeAllOnScrollChangeListener(){\n        mOnScrollChangeListeners.clear();\n    }\n\n    @Override\n    protected void onScrollChanged(int l, int t, int oldl, int oldt) {\n        super.onScrollChanged(l, t, oldl, oldt);\n        for (OnScrollChangeListener onScrollListener : mOnScrollChangeListeners) {\n            onScrollListener.onScrollChange(this, l, t, oldl, oldt);\n        }\n    }\n\n    @Override\n    public void setWebViewClient(WebViewClient client) {\n        if (client != null && !(client instanceof QMUIWebViewClient)) {\n            throw new IllegalArgumentException(\"must use the instance of QMUIWebViewClient\");\n        }\n        super.setWebViewClient(client);\n    }\n\n    @Override\n    public boolean dispatchKeyEvent(KeyEvent event) {\n        return super.dispatchKeyEvent(event);\n    }\n\n    public void setNeedDispatchSafeAreaInset(boolean needDispatchSafeAreaInset) {\n        if (mNeedDispatchSafeAreaInset != needDispatchSafeAreaInset) {\n            mNeedDispatchSafeAreaInset = needDispatchSafeAreaInset;\n            if (ViewCompat.isAttachedToWindow(this)) {\n                if (needDispatchSafeAreaInset) {\n                    ViewCompat.requestApplyInsets(this);\n                } else {\n                    // clear insets\n                    setStyleDisplayCutoutSafeArea(new Rect());","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/Tencent/QMUI_Android/blob/026e7d486677d6593a96689cd590ec3561176717/qmui/src/main/java/com/qmuiteam/qmui/widget/webview/QMUIWebView.java#L112-L148","documentation":"QMUIWebView overrides setWebViewClient to enforce that only its own QMUIWebViewClient (or a subclass) is installed, because QMUI's internal features (load progress callbacks, bridge handling) rely on hooks inside QMUIWebViewClient. Passing any other WebViewClient is rejected with an IllegalArgumentException; null is still allowed.","triggerScenarios":"Calling webView.setWebViewClient(new WebViewClient(){...}) (or any non-QMUIWebViewClient instance, non-null) on a QMUIWebView instance.","commonSituations":"Migrating existing WebView code to QMUIWebView by copying over the standard Android setWebViewClient call; tutorials/snippets for android.webkit.WebView applied verbatim to QMUIWebView.","solutions":["Subclass QMUIWebViewClient instead of WebViewClient and pass that instance to setWebViewClient.","Override the needed callbacks (shouldOverrideUrlLoading, onPageFinished, etc.) in your QMUIWebViewClient subclass.","If you do not need QMUIWebView's extensions, use a plain android.webkit.WebView instead."],"exampleFix":"// before\nwebView.setWebViewClient(new WebViewClient() {\n    @Override public boolean shouldOverrideUrlLoading(WebView v, String url) { return false; }\n});\n\n// after\nwebView.setWebViewClient(new QMUIWebViewClient(false, false) {\n    @Override\n    public boolean shouldOverrideUrlLoading(WebView v, String url) { return false; }\n});","handlingStrategy":"type-guard","validationCode":"if (client != null && !(client instanceof QMUIWebViewClient)) {\n    throw new IllegalArgumentException(\"setWebViewClient requires a QMUIWebViewClient\");\n}","typeGuard":"// Java has no runtime narrowing sugar; use an explicit check helper\nstatic boolean isValidClient(WebViewClient c) {\n    return c == null || c instanceof QMUIWebViewClient;\n}","tryCatchPattern":"try {\n    webView.setWebViewClient(client);\n} catch (IllegalArgumentException e) {\n    webView.setWebViewClient(new QMUIWebViewClient(false, false));\n}","preventionTips":["Always subclass QMUIWebViewClient for QMUIWebView instances.","Centralize webview setup in one factory so no plain WebViewClient can be installed.","Prefer using QMUIWebView's own client (it sets one internally) unless you need custom callbacks."],"tags":["android","webview","type-constraint","illegal-argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"026e7d486677d6593a96689cd590ec3561176717","analyzedAt":"2026-09-06T13:32:24.816Z","contentChangedAt":"2026-09-06T13:32:24.816Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}