Webview crashes when displaying a youtube video

半世苍凉 提交于 2019-12-11 07:43:57

问题


After updating to 8.0 we get a crash so far we haven't seen before:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
                                                                 at com.android.webview.chromium.WebViewContentsClientAdapter.getDefaultVideoPoster(WebViewContentsClientAdapter.java:536)
                                                                 at org.chromium.android_webview.DefaultVideoPosterRequestHandler$1.run(DefaultVideoPosterRequestHandler.java:2)
                                                                 at android.os.Handler.handleCallback(Handler.java:789)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                 at android.os.Looper.loop(Looper.java:164)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                 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)

Is this a bug in Chrome?


回答1:


Thanks to @breakline's answer, I got this issue worked around! Thanks! But instead of using decoding a bitmap, I just create an empty one and return that:

    setWebChromeClient(new WebChromeClient() {
        @Override
        public Bitmap getDefaultVideoPoster() {
            return Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
        }
    });



回答2:


Anyone still wondering instead of downvoting here is the actual fix to this. Since I spent quite some time on this I will post it here.

You need to add the following to your WebChromeClient:

webview.setWebChromeClient(new WebChromeClient() {
        @Override
        public Bitmap getDefaultVideoPoster() {
            Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.black);
            return icon;
        }
    });

Any drawable is fine as long as it is an actual PNG file. If you use a drawable/xml you still get an exception.



来源:https://stackoverflow.com/questions/46886701/webview-crashes-when-displaying-a-youtube-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!