我正在尝试将用JavaFX编写的应用程序移植到Android上,并且在大多数情况下,我的尝试都取得了成功。在使用Netbeans IDE和Gradle打包apk文件后,应用程序将在Android上运行。
但我遇到的问题是,当尝试通过JavaFX Webview组件访问谷歌地图时,应用程序将崩溃。我正在使用的是JavaFX中的以下内容:
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$1.handle(BehaviorSkinBase.java:96)
E/AndroidRuntime( 6038): at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.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.access$1500(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$handleMouseEvent$336(GlassViewEventHandler.java:388)
E/AndroidRuntime( 6038): at com.sun.javafx.tk.quantum.GlassViewEventHandler.access$lambda$1(GlassViewEventHandler.java)
E/AndroidRuntime( 6038): at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$4.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
html文件位于Gradle根文件夹的资源文件夹下。
当我在Netbeans中测试应用程序时,右键单击Gradle根节点并选择Tasks - >运行 - >跑。我的应用程序运行完美,包括使用html文件和JavaFX Webview访问谷歌地图。
创建apk文件也成功使用了任务 - > Android - > Android系统。但是,一旦我在Android设备上安装apk文件并访问我的应用程序的谷歌地图部分,它就会让我退出应用程序 说“不幸已停止”。关于如何解决这个问题的任何线索?
查看使用adb命令创建的日志文件,我看到了以下输出:
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/'
}
}
这是build.gradle文件,是的,我使用的是Verdana字体。但是从我在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());
此代码块在Android中出现错误消息:
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);");
}
}
});
网页不可用: jar的网页:file:/data/app/com.SundaeThePugApp - 1 / base .apk!/googleLondonMap.html 无法加载,因为:
净:: ERR_UNKNOWN_URL_SCHEME
所以我现在可以做些什么来纠正这个问题?
再次编辑
尝试使用Jose解决方案并在其基础上进行构建,我发现了一种在Html中使用JavaScript的方法,尽管它并不漂亮。基本上他提供的代码我放入一个方法(称为createAndroidURLScheme)并返回一个字符串。这是代码,是的,它在JavaFX中工作正常,但在Androids webview中再次没有:
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.access$400(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$1.changed(SundaeThePugController.java:202)
E/AndroidRuntime( 809): at com.SundaeThePugApp.SundaeThePugController$1.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.access$1000(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$1.run(NativeWebView.java:100)
E/AndroidRuntime( 809): at com.sun.javafx.application.PlatformImpl.lambda$null$155(PlatformImpl.java:295)
E/AndroidRuntime( 809): at com.sun.javafx.application.PlatformImpl.access$lambda$18(PlatformImpl.java)
E/AndroidRuntime( 809): at com.sun.javafx.application.PlatformImpl$$Lambda$19.run(Unknown Source)
E/AndroidRuntime( 809): at java.security.AccessController.doPrivileged(AccessController.java:52)
E/AndroidRuntime( 809): at com.sun.javafx.application.PlatformImpl.lambda$runLater$156(PlatformImpl.java:294)
E/AndroidRuntime( 809): at com.sun.javafx.application.PlatformImpl.access$lambda$5(PlatformImpl.java)
E/AndroidRuntime( 809): at com.sun.javafx.application.PlatformImpl$$Lambda$6.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
现在通过logcat我得到了这个新的Android Fatal Exception:
{{1}}
我现在可以做些什么来缓解这种情况并让Androids Webview感到高兴?