JavaFX, Gluon : HTML file not loaded from Java cod

2019-09-11 10:56发布

问题:

I have a Gluon JavafX project on Intellij Idea after installing Gluon plugin, in which I am loading some static HTML files in JavaFX's Webkit browser.

Now, when I start the JavaFX application directly, I can see the HTML being loaded, but when I create the APK, I don't see the HTML being loaded. I tried two paths i.e html/index.html and index.html, but for both the APK didn't show me anything in the app, it just loads a black screen.

As per the previous question I had about Gluon, I have moved static content in resources directory. This increased the size of APK a lot and that was a confirmation that the static content is inside the APK.

Here is the Main class :

@Override
public void start(Stage primaryStage) throws Exception{
    primaryStage.setTitle("Rewe - Frontend");
    java.net.CookieManager manager = new java.net.CookieManager();
    java.net.CookieHandler.setDefault(manager);

    myBrowser = new MyBrowser();
    scene = new Scene(myBrowser, 1080, 1920);

    primaryStage.setScene(scene);
    primaryStage.setFullScreen(true);
    primaryStage.show();

}

Mybrowser.Java :

public class MyBrowser extends Region {

    final String hellohtml = "index.html";

    WebView webView = new WebView();
    WebEngine webEngine = webView.getEngine();
public MyBrowser() {
        webEngine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue == Worker.State.SUCCEEDED) {
                JSObject window = (JSObject) webEngine.executeScript("window");
                window.setMember("app", this);
            }
        });

        URL urlHello = getClass().getResource(hellohtml);

        webEngine.load(urlHello.toExternalForm());
        webView.setPrefSize(1080, 1920);
        webView.setContextMenuEnabled(false);
}

Here is the screenshot showing static files in appropriate folders and Java files as well :

Then why is that when I open the APK I just see a black screen. I just put "Hello world" in body of index.html, even that is not shown. Any help would be nice. Thank you.

Update

Error log with adb :

: QuantumRenderer: shutdown
02-12 12:49:24.539 13109 13132 W System.err: java.lang.reflect.InvocationTargetException
02-12 12:49:24.540 13109 13132 W System.err:    at java.lang.reflect.Method.invoke(Native Method)
02-12 12:49:24.540 13109 13132 W System.err:    at javafxports.android.DalvikLauncher$1.run(DalvikLauncher.java:188)
02-12 12:49:24.540 13109 13132 W System.err:    at java.lang.Thread.run(Thread.java:818)
02-12 12:49:24.540 13109 13132 W System.err: Caused by: java.lang.RuntimeException: Exception in Application start method
02-12 12:49:24.540 13109 13132 W System.err:    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
02-12 12:49:24.540 13109 13132 W System.err:    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$137(LauncherImpl.java:182)
02-12 12:49:24.540 13109 13132 W System.err:    at com.sun.javafx.application.LauncherImpl.access$lambda$1(LauncherImpl.java)
02-12 12:49:24.540 13109 13132 W System.err:    at com.sun.javafx.application.LauncherImpl$$Lambda$2.run(Unknown Source)
02-12 12:49:24.540 13109 13132 W System.err:    ... 1 more
02-12 12:49:24.540 13109 13132 W System.err: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.net.URL.toExternalForm()' on a null object reference
02-12 12:49:24.541 13109 13132 W System.err:    at com.rewetest.MyBrowser.<init>(MyBrowser.java:69)
02-12 12:49:24.541 13109 13132 W System.err:    at com.rewetest.Main.start(Main.java:30)
02-12 12:49:24.541 13109 13132 W System.err:    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$144(LauncherImpl.java:863)
02-12 12:49:24.541 13109 13132 W System.err:    at com.sun.javafx.application.LauncherImpl.access$lambda$8(LauncherImpl.java)
02-12 12:49:24.541 13109 13132 W System.err:    at com.sun.javafx.application.LauncherImpl$$Lambda$9.run(Unknown Source)
02-12 12:49:24.541 13109 13132 W System.err:    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$157(PlatformImpl.java:326)

回答1:

If you use

webEngine.load(getClass().getResource("index.html").toExternalForm()); 

you need to put the resources in the same package as that class:

src/main/resources/com/<your package>

or if you want to put it in a different folder, use "/" as root of the resources:

webEngine.load(getClass().getResource("/html/index.html").toExternalForm());

This will solve the NPE.

I don't see it in your code, but I take that you are adding the WebView to the scene:

public MyBrowser() {
     ...
     getChildren().add(webView);
}