我几天来一直在研究这个问题,并且无法解决这个问题。我在使用WebViews时遇到的问题是,当我加载vimeo视频时,它将失败并出现此错误:
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:1181)
at org.chromium.android_webview.DefaultVideoPosterRequestHandler$1.run(DefaultVideoPosterRequestHandler.java:39)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
现在奇怪的是,这种情况发生在Anroid 7.0上并没有比Android 4.4更低(没有5.0或6.0的物理设备可以测试)。
这是我正在运行以呈现片段的代码:
public class FragmentWebView extends Fragment{
protected WebView myWebView;
private String title, url, message;
private boolean isOffline;
View vWebView;
ViewFinder viewPullDowns;
SwipeRefreshLayout srlWebview;
protected void setTitle(String title) {
this.title = title;
}
protected void setUrl(String url) {
this.url = url;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
vWebView = inflater.inflate(R.layout.fragment_web_frame, container, false);
((MainActivity)getActivity()).setToClose();
((MainActivity)getActivity()).setTitle(title);
init();
return vWebView;
}
private void init() {
viewPullDowns = new ViewFinder((ViewGroup)vWebView, "pulldown");
srlWebview = (SwipeRefreshLayout) vWebView.findViewById(R.id.srl_fwf_webview);
if(message == null) {
message = "Unable to connect";
}
initWebView();
srlWebview.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
if(url != null) {
myWebView.loadUrl(url);
}
}
});
}
private void initWebView() {
myWebView = (WebView) vWebView.findViewById(R.id.webview);
if(url != null) {
try {
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowContentAccess(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl(url);
} catch (NullPointerException e) {
} catch (Exception e) {
}
}else{
initOffline();
}
}
protected void initOffline() {
myWebView.setBackgroundColor(getResources().getColor(R.color.colorTransparent));
viewPullDowns.setAllViewsVisibility(View.VISIBLE);
((MainActivity)getActivity()).showMessageOnBottom(message, R.color.colorError);
((MainActivity)getActivity()).hideSubmenu();
isOffline = true;
srlWebview.setRefreshing(false);
}
}
偶尔会出现此错误,如果我卸载我的应用程序,它将再次开始工作。似乎WebView正在缓存null对象然后加载它们(不是100%肯定)。有没有其他人在此之前遇到此问题或者已经研究过支持在片段内部浏览网页的替代库?