I use Webview and got a crash as log below

FATAL EXCEPTION: main
Process: com.mbc.qc, PID: 30465
                                                   java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
                                                       at com.android.webview.chromium.Ap.getDefaultVideoPoster(WebViewContentsClientAdapter.java:565)
                                                       at org.chromium.android_webview.DefaultVideoPosterRequestHandler$$Lambda$0.run(Unknown Source:2)
                                                       at android.os.Handler.handleCallback(Handler.java:789)
                                                       at android.os.Handler.dispatchMessage(Handler.java:98)
                                                       at android.os.Looper.loop(Looper.java:172)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6637)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

On first line, it show Bitmap.getWidth() on null pointer but I did not use any Bitmap on my webview. So this log make me confuse.

Below is my code for webview

WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowContentAccess(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setDomStorageEnabled(true);     
        webSettings.setLoadsImagesAutomatically(true);


webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    webSettings.setPluginState(WebSettings.PluginState.ON);

    webView.addJavascriptInterface(new WebViewResizer(), "WebViewResizer");
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView webView, String url) {
            super.onPageFinished(webView, url);
            System.out.println("WEBVIEW LOAD PAGE FINISHED");
        }
    });
    webView.setWebChromeClient(new WebChromeClient());
    webView.loadDataWithBaseURL(null, URL, "text/html; charset=utf-8", null, "");

I tried to remove the line set chrome client but it did not help.

webView.setWebChromeClient(new WebChromeClient());

URL is a script like this:

 myScript = "<iframe width=\"500\" height=\"402\" src=\"https://www.youtube.com/embed/y7tEBhRp-MA\" frameborder=\"0\" gesture=\"media\" allow=\"encrypted-media\" allowfullscreen></iframe>";

Does anyone get a same issue like me and how you resolve it? Thanks.

1 Answers

0
Ozan On

Try my own implementation. Add below code to your CustomChromeClient;

@Nullable
@Override
public Bitmap getDefaultVideoPoster() {
    if (super.getDefaultVideoPoster() == null) {
        return BitmapFactory.decodeResource(context.getResources(),
                R.drawable.ic_launcher);
    } else {
        return super.getDefaultVideoPoster();
    }
}