将 JavaFX 应用程序转换为 Android APK - HTML 文件加载问题 Google 地图

Converting JavaFX application into Android APK - issues with HTML file loading Google Maps

我正在尝试将用 JavaFX 编写的应用程序移植到 Android,并且在大多数情况下,我的尝试都取得了成功。在使用 Netbeans IDE 和 Gradle.

打包 apk 文件后,应用程序将 运行 on Android

但我遇到的问题是,当尝试通过 JavaFX Webview 组件访问 google 地图时,应用程序会崩溃。我在 JavaFX 中使用的是以下内容:

googleMapEngine.load(getClass().getResource("/googleLondonMap.html").toExternalForm());

并且 html 文件位于 Gradle 根文件夹的 Resources 文件夹下。

当我通过右键单击 Gradle 根节点并选择任务 --> 运行 --> 运行 在 Netbeans 中测试应用程序时。我的应用程序 运行 非常完美,包括使用 html 文件和 JavaFX Webview 访问 google 地图。

apk文件的创建也是使用Tasks --> Android --> Android成功。但是一旦我在 android 设备上安装 apk 文件并访问我的应用程序的 google 地图部分,它就会让我退出应用程序 说 "Unfortunately has stopped"。关于如何解决这个问题的任何线索?

查看使用 adb 命令创建的日志文件,我看到了以下输出:

E/AndroidRuntime( 6038): FATAL EXCEPTION: JavaFX Application Thread

E/AndroidRuntime( 6038): Process: com.SundaeThePugApp, PID: 6038

E/AndroidRuntime( 6038): java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

E/AndroidRuntime( 6038):    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)

E/AndroidRuntime( 6038):    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)

E/AndroidRuntime( 6038):    at javafx.event.Event.fireEvent(Event.java:198)

E/AndroidRuntime( 6038):    at javafx.scene.Node.fireEvent(Node.java:8411)

E/AndroidRuntime( 6038):    at javafx.scene.control.Button.fire(Button.java:185)

E/AndroidRuntime( 6038):    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)

E/AndroidRuntime( 6038):    at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:96)

E/AndroidRuntime( 6038):    at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:89)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)

E/AndroidRuntime( 6038):    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)

E/AndroidRuntime( 6038):    at javafx.event.Event.fireEvent(Event.java:198)

E/AndroidRuntime( 6038):    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)

E/AndroidRuntime( 6038):    at javafx.scene.Scene$MouseHandler.access00(Scene.java:3485)

E/AndroidRuntime( 6038):    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)

E/AndroidRuntime( 6038):    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)

E/AndroidRuntime( 6038):    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)

E/AndroidRuntime( 6038):    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)

E/AndroidRuntime( 6038):    at java.security.AccessController.doPrivileged(AccessController.java:52)

E/AndroidRuntime( 6038):    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent6(GlassViewEventHandler.java:388)

E/AndroidRuntime( 6038):    at com.sun.javafx.tk.quantum.GlassViewEventHandler.access$lambda(GlassViewEventHandler.java)

E/AndroidRuntime( 6038):    at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda.get(Unknown Source)

E/AndroidRuntime( 6038):    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)

E/AndroidRuntime( 6038):    at com.sun.javafx.tk.quantum.GlassViewE

这是 build.gradle 文件,是的,我使用的是 Verdana 字体。但是从我在 android 设备上看到的字体来看,字体是正常的。

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.javafxports:jfxmobile-plugin:1.0.7'
    }
}

apply plugin: 'org.javafxports.jfxmobile'

repositories {
    jcenter()
    maven {
        url 'http://nexus.gluonhq.com/nexus/content/repositories/releases'
    }
}

mainClassName = 'com.SundaeThePugApp.SundaeThePugFX'

dependencies {
    compile 'com.gluonhq:charm:2.1.0'

    androidRuntime 'com.gluonhq:charm-android:2.1.0'
    iosRuntime 'com.gluonhq:charm-ios:2.1.0'
    desktopRuntime 'com.gluonhq:charm-desktop:2.1.0'
}

jfxmobile {
    android {
        manifest = 'src/android/AndroidManifest.xml'
        androidSdk = 'C:/Users/Zermatt/AppData/Local/Android/android-sdk/'
    }
}

编辑

找到致命异常的原因。看来 Android 不喜欢下面的代码块:

URL resource= getClass().getClassLoader().getResource("Music/ThemeMusic.mp3");
        themeSong = new MediaPlayer(new Media(resource.toString()));
        themeSong.setOnEndOfMedia(new Runnable()
        {   public void run()
            {   themeSong.seek(Duration.ZERO);
            }
        });
        themeSong.play();

并且此代码块在 Android 中出现错误消息:

googleMapEngine.load(getClass().getResource("/googleLondonMap.html").toExternalForm());

网页不可用: 位于 jar:file:/data/app/com.SundaeThePugApp - 1/base .apk!/googleLondonMap.html 的网页 无法加载,因为:

网络::ERR_UNKNOWN_URL_SCHEME

那么我现在可以做些什么来纠正这个问题?

再次编辑

试验 Jose 解决方案并以此为基础,我找到了一种使 Html 中的 JavaScript 正常工作的方法,尽管它并不漂亮。基本上我将他提供的代码放入一个方法(称为 createAndroidURLScheme)并返回一个字符串。这是代码,是的,它在 JavaFX 中工作正常,但在 Androids webview 中又不行了:

String url = createAndroidURLScheme("/googleTestMap.html");
//System.out.println(url);
googleMapEngine.loadContent(url, "text/html");
googleMapEngine.setJavaScriptEnabled(true);
                googleMapEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>()
                {   public void changed(final ObservableValue<? extends Worker.State> observableValue, final State oldState, final State newState)
                    {   if (newState == State.SUCCEEDED)
                        {   googleMapEngine.executeScript(
                                "var latlng = new google.maps.LatLng(59.438722, 24.745278);" +
                                "var myOptions = {" + 
                                "zoom: 15," +
                                "center: latlng," +
                                "mapTypeId: google.maps.MapTypeId.ROADMAP," +
                                "mapTypeControl: false," +
                                "navigationControl: false," +
                                "streetViewControl: false," +
                                "backgroundColor: '#666970'," +
                                "disableDefaultUI: true" +
                                "};" +
                                "" +
                                "var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);");
                        }
                    }
                });

现在通过 logcat 我得到了这个新的 Android 致命异常:

    I/System.out(  809): [JVDBG] Error firing event

W/System.err(  809): java.lang.ClassNotFoundException: com.sun.webkit.NativeWebView

W/System.err(  809):    at java.lang.Class.classForName(Native Method)

W/System.err(  809):    at java.lang.Class.forName(Class.java:309)

W/System.err(  809):    at com.oracle.dalvik.InternalWebView._fireLoadEvent(InternalWebView.java:347)

W/System.err(  809):    at com.oracle.dalvik.InternalWebView.fireLoadEvent(InternalWebView.java:140)

W/System.err(  809):    at com.oracle.dalvik.InternalWebView.access0(InternalWebView.java:43)

W/System.err(  809):    at com.oracle.dalvik.InternalWebView$MyJavaScriptInterface.processHTML(InternalWebView.java:363)

W/System.err(  809):    at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)

W/System.err(  809):    at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:39)

W/System.err(  809):    at android.os.Handler.dispatchMessage(Handler.java:102)

W/System.err(  809):    at android.os.Looper.loop(Looper.java:145)

W/System.err(  809):    at android.os.HandlerThread.run(HandlerThread.java:61)

W/System.err(  809): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.sun.webkit.NativeWebView" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/vendor/lib, /system/lib]]

W/System.err(  809):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)

W/System.err(  809):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)

W/System.err(  809):    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)

W/System.err(  809):    ... 11 more

W/System.err(  809):    Suppressed: java.lang.ClassNotFoundException: com.sun.webkit.NativeWebView

W/System.err(  809):        at java.lang.Class.classForName(Native Method)

W/System.err(  809):        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)

W/System.err(  809):        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)

W/System.err(  809):        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)

W/System.err(  809):        ... 12 more

W/System.err(  809):    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

E/AndroidRuntime(  809): FATAL EXCEPTION: JavaFX Application Thread

E/AndroidRuntime(  809): Process: com.SundaeThePugApp, PID: 809

E/AndroidRuntime(  809): java.lang.UnsupportedOperationException: Not supported yet.

E/AndroidRuntime(  809):    at com.sun.webkit.WebPage.executeScript(WebPage.java:153)

E/AndroidRuntime(  809):    at javafx.scene.web.WebEngine.executeScript(WebEngine.java:860)

E/AndroidRuntime(  809):    at com.SundaeThePugApp.SundaeThePugController.changed(SundaeThePugController.java:202)

E/AndroidRuntime(  809):    at com.SundaeThePugApp.SundaeThePugController.changed(SundaeThePugController.java:200)

E/AndroidRuntime(  809):    at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)

E/AndroidRuntime(  809):    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)

E/AndroidRuntime(  809):    at javafx.beans.property.ReadOnlyObjectWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyObjectWrapper.java:176)

E/AndroidRuntime(  809):    at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:142)

E/AndroidRuntime(  809):    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)

E/AndroidRuntime(  809):    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)

E/AndroidRuntime(  809):    at javafx.scene.web.WebEngine$LoadWorker.updateState(WebEngine.java:1023)

E/AndroidRuntime(  809):    at javafx.scene.web.WebEngine$LoadWorker.dispatchLoadEvent(WebEngine.java:1134)

E/AndroidRuntime(  809):    at javafx.scene.web.WebEngine$LoadWorker.access00(WebEngine.java:1016)

E/AndroidRuntime(  809):    at javafx.scene.web.WebEngine$PageLoadListener.dispatchLoadEvent(WebEngine.java:1003)

E/AndroidRuntime(  809):    at com.sun.webkit.WebPage.fireLoadEvent(WebPage.java:251)

E/AndroidRuntime(  809):    at com.sun.webkit.NativeWebView.run(NativeWebView.java:100)

E/AndroidRuntime(  809):    at com.sun.javafx.application.PlatformImpl.lambda$null5(PlatformImpl.java:295)

E/AndroidRuntime(  809):    at com.sun.javafx.application.PlatformImpl.access$lambda(PlatformImpl.java)

E/AndroidRuntime(  809):    at com.sun.javafx.application.PlatformImpl$$Lambda.run(Unknown Source)

E/AndroidRuntime(  809):    at java.security.AccessController.doPrivileged(AccessController.java:52)

E/AndroidRuntime(  809):    at com.sun.javafx.application.PlatformImpl.lambda$runLater6(PlatformImpl.java:294)

E/AndroidRuntime(  809):    at com.sun.javafx.application.PlatformImpl.access$lambda(PlatformImpl.java)

E/AndroidRuntime(  809):    at com.sun.javafx.application.PlatformImpl$$Lambda.run(Unknown Source)

E/AndroidRuntime(  809):    at com.sun.glass.ui.monocle.RunnableProcessor.runLoop(RunnableProcessor.java:92)

E/AndroidRuntime(  809):    at com.sun.glass.ui.monocle.RunnableProcessor.run(RunnableProcessor.java:51)

E/AndroidRuntime(  809):    at java.lang.Thread.run(Thread.java:818)

V/ApplicationPolicy( 3468): isApplicationStateBlocked userId 0 pkgname com.SundaeThePugApp

V/ApplicationPolicy( 3468): isApplicationStateBlocked userId 0 pkgname com.SundaeThePugApp

V/ApplicationPolicy( 3468): isApplicationStateBlocked userId 0 pkgname com.sec.android.app.launcher

我现在可以做些什么来缓解这个问题并保持 Android 的 Webview 快乐?

根据提供的最新信息,您有两个已知问题:

媒体

如果您查看 this,媒体尚不支持,因此您需要将其删除。

Url方案错误

如果您查看错误消息 jar:file:/data/.../base.apk!/googleLondonMap.html 显示的 URLnet::ERR_UNKNOWN_URL_SCHEME 表示 jar:file 不是 [= 上的已知协议38=].

原因是 android SDK 不支持从 jar 文件加载 html 文件。

无论如何,该资源是可以访问的,因此作为一种解决方法,您可以将 html 文件加载到一个字符串中,然后使用方法 loadContent 将其提供给 WebEngine .

像这样的东西应该可以工作:

StringWriter writer = new StringWriter();
BufferedReader reader = null;
try {
    reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream("/googleLondonMap.html")));
    String line = null;
    while ((line = reader.readLine()) != null) {
        writer.append(line);
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (reader != null) {
        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
engine.loadContent(writer.toString());