WebView在Bitmap.getWidth()处由空指针崩溃

时间:2018-01-16 21:41:43

标签: android webview

我使用WebView并在下面的日志中崩溃

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)

在第一行,它在空指针上显示Bitmap.getWidth(),但我没有在我的webview上使用任何位图。

以下是我的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, "");

我尝试删除行集Chrome客户端,但它没有帮助。

webView.setWebChromeClient(new WebChromeClient());

网址是这样的脚本:

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

有没有人像我一样得到同样的问题以及你如何解决它?

1 个答案:

答案 0 :(得分:0)

尝试我自己的实现。将以下代码添加到您的CustomChromeClient;

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