使用JavaFX的WebView NullPointerException [英] WebView NullPointerException using JavaFX

查看:113
本文介绍了使用JavaFX的WebView NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近将WebView窗格添加到我的JavaFX Application中.调整程序大小时,出现此错误,程序冻结.

Recently adding a WebView pane to my JavaFX Application. When resizing my program I get this error and the program freezes.

java.lang.NullPointerException
    at com.sun.javafx.webkit.prism.WCPageBackBufferImpl.validate(WCPageBackBufferImpl.java:97)
    at com.sun.webkit.WebPage.paint(WebPage.java:644)
    at com.sun.javafx.sg.prism.NGWebView.renderContent(NGWebView.java:95)
    at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2067)
    at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1959)
    at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:235)
    at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:576)
    at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2067)
    at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1959)
    at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:235)
    at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:576)
    at com.sun.javafx.sg.prism.NGNode.renderForClip(NGNode.java:2308)
    at com.sun.javafx.sg.prism.NGNode.renderRectClip(NGNode.java:2202)
    at com.sun.javafx.sg.prism.NGNode.renderClip(NGNode.java:2228)
    at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2061)
    at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1959)
    at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:235)
    at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:576)
    at com.sun.javafx.sg.prism.NGNode.renderForClip(NGNode.java:2308)
    at com.sun.javafx.sg.prism.NGNode.renderRectClip(NGNode.java:2202)
    at com.sun.javafx.sg.prism.NGNode.renderClip(NGNode.java:2228)
    at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2061)
    at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1959)
    at com.sun.javafx.tk.quantum.ViewPainter.doPaint(ViewPainter.java:474)
    at com.sun.javafx.tk.quantum.ViewPainter.paintImpl(ViewPainter.java:327)
    at com.sun.javafx.tk.quantum.PresentingPainter.run(PresentingPainter.java:91)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
    at com.sun.javafx.tk.RenderJob.run(RenderJob.java:58)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:125)
    at java.lang.Thread.run(Thread.java:745)

Webview所访问的网站是

The website that the webview goes to is

http://www.abendigo.org/gui/

有人可以告诉我是什么原因引起的吗?

Can someone tell me what causes this issue?

不确定为什么需要这样做,但这是所要求的代码:

Not sure why this is needed, but here is the code as requested:

public class Controller implements Initializable {

    @FXML
    public WebView webView;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        webView.getEngine().load("http://abendigo.org/gui/");
    }

}

错误再现的视频: https://youtu.be/6CLPt45AbW0

推荐答案

我遇到了相同的错误.很难找到原因. 最后,我发现:当宽度或高度太大(即超过8500像素)时,webView布局会抛出nullPointerExpection.

I was faced with the same bug. It was quite difficult to find the reason. At last, I found : webView layout throw a nullPointerExpection when width or height is too large, ie over 8500 pixel.

良好的旁路功能太过限制了webView的maxWidth/maxHeight到场景大小:

And the good bypass was too limit the maxWidth/maxHeight of the webView to scene size :

 webView.sceneProperty().addListener(new ChangeListener<Scene>() {

        @Override
        public void changed(ObservableValue<? extends Scene> observable, Scene oldValue, Scene scene) {
            if (scene != null) {
                webView.setMaxSize(getScene().getWidth(), getScene().getHeight());
                webView.maxWidthProperty().bind(getScene().widthProperty());
                webView.maxHeightProperty().bind(getScene().heightProperty());
            } else {
                webView.maxWidthProperty().unbind();
                webView.maxHeightProperty().unbind();
            }
        }
    });

这篇关于使用JavaFX的WebView NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆