我想问一下,如果有人在使用Gluon Charm Down插件进行IOS屏幕定位时遇到问题吗?
JFXMobile插件:org.javafxports:jfxmobile-plugin:1.3.2
魅力版:com.gluonhq:charm:4.2.0
downConfig {
version = '3.1.0'
plugins 'display', 'lifecycle', 'statusbar', 'storage', 'orientation'
}
当我试着打电话时,就像这样:
Services.get(OrientationService.class).ifPresent(service -> {
onOrientationChange(service.orientationProperty(), null, service.getOrientation().orElse(Orientation.VERTICAL));
service.orientationProperty().addListener(this::onOrientationChange);
});
我在控制台上遇到异常:
Exception in Preloader start method
2017-02-06 10:43:37.104693 MyApp[447:282589] Orientation is Unknown
QuantumRenderer: shutdown
java.lang.RuntimeException: Exception in Preloader start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(LauncherImpl.java)
at com.sun.javafx.application.LauncherImpl$$Lambda$2.run(Unknown Source)
at java.lang.Thread.run(Thread.java)
Caused by: java.lang.NullPointerException
at com.gluonhq.charm.down.plugins.ios.IOSOrientationService.getOrientation(IOSOrientationService.java)
at my.app.Preloader.lambda$start$24(Preloader.java)
at my.app.Preloader$$Lambda$3.accept(Unknown Source)
at java.util.Optional.ifPresent(Optional.java)
at my.app.Preloader.start(Preloader.java)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java)
at com.sun.javafx.application.LauncherImpl$$Lambda$7.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java)
at com.sun.javafx.application.PlatformImpl$$Lambda$7.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java)
at com.sun.javafx.application.PlatformImpl$$Lambda$19.run(Unknown Source)
at java.security.AccessController.doPrivileged(AccessController.java)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java)
at com.sun.javafx.application.PlatformImpl$$Lambda$6.run(Unknown Source)
at org.robovm.apple.uikit.UIApplication.main(UIApplication.java)
at org.robovm.apple.uikit.UIApplication.main(UIApplication.java)
at org.javafxports.jfxmobile.ios.BasicLauncher.main(BasicLauncher.java)
查看code,我认为这个问题只有一个原因:
@Override
public final Optional<Orientation> getOrientation() {
switch (orientationText) {
case "Portrait":
case "PortraitUpsideDown":
return Optional.of(Orientation.VERTICAL);
case "LandscapeLeft":
case "LandscapeRight":
return Optional.of(Orientation.HORIZONTAL);
case "Unknown":
default:
return Optional.empty();
}
}
我的猜测是,orientationText
是null
,因此会崩溃。
我想,行2017-02-06 10:43:37.104693 MyApp[447:282589] Orientation is Unknown
对此有所贡献。
这是一个错误吗?有办法绕过这个吗? (例如,IOS上是否需要一些设置,例如Android上的权限系统?)
提前致谢并致谢,
丹尼尔
#edit: onOrientationChange
方法并不复杂:
private void onOrientationChange(ObservableValue<? extends Orientation> obs, Orientation o, Orientation n) {
if (n == null || splashPane == null)
return;
splashPane.pseudoClassStateChanged(vertical, Orientation.VERTICAL == n);
splashPane.pseudoClassStateChanged(horizontal, Orientation.HORIZONTAL == n);
}
所以我想将代码更新为某事就足够了。像这样
Services.get(OrientationService.class).ifPresent(service -> {
service.orientationProperty()的addListener(此:: onOrientationChange)。 });
(它在Android上工作,所以我可以选择检查平台,只在非IOS上执行此操作)
答案 0 :(得分:1)
我可以重现你的NPE。
当您急切地发生错误时,就会发生错误。致电service.getOrientation()
。
如果查看code,orientationText
未初始化,只有在初始化服务并且观察者启动时才会从iOS本机层检索值并设置与Platform::runLater
。
这意味着获取orientationText
的初始值需要一段时间。
避免使用NPE的快速解决方案是使用常规ChangeListener<Observation>
代替onOrientationChange
方法:
private final ChangeListener<Orientation> onOrientationChange =
(ObservableValue<? extends Orientation> obs, Orientation ov, Orientation nv) -> {
if (nv == null || splashPane == null)
return;
splashPane.pseudoClassStateChanged(vertical, Orientation.VERTICAL == nv);
splashPane.pseudoClassStateChanged(horizontal, Orientation.HORIZONTAL == nv);
}
Services.get(OrientationService.class).ifPresent(service -> {
service.orientationProperty().addListener(onOrientationChange);
});
无论如何,我都会向orientationText
提交一个初始值的问题。