从 Gluon Charm Down 读取屏幕方向的空指针
Null Pointer on reading the screen orientation from Gluon Charm Down
我想问一下,是否有人在使用 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$launchApplication6(LauncherImpl.java)
at com.sun.javafx.application.LauncherImpl$$Lambda.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(Preloader.java)
at my.app.Preloader$$Lambda.accept(Unknown Source)
at java.util.Optional.ifPresent(Optional.java)
at my.app.Preloader.start(Preloader.java)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication11(LauncherImpl.java)
at com.sun.javafx.application.LauncherImpl$$Lambda.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.java)
at com.sun.javafx.application.PlatformImpl$$Lambda.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java)
at com.sun.javafx.application.PlatformImpl$$Lambda.run(Unknown Source)
at java.security.AccessController.doPrivileged(AccessController.java)
at com.sun.javafx.application.PlatformImpl.lambda$runLater5(PlatformImpl.java)
at com.sun.javafx.application.PlatformImpl$$Lambda.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(this::onOrientationChange);
});
(它在 Android 上运行,所以我可以选择检查平台,并且只在非 IOS 左右的平台上运行)
我可以重现你的NPE。
错误发生在您 'eagerly' 调用 service.getOrientation()
时。
如果你看一下code,orientationText
没有初始化,只有当服务初始化和观察者启动时,才会从[=32中检索值=] 原生图层并设置 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
添加一个初始值。
我想问一下,是否有人在使用 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$launchApplication6(LauncherImpl.java)
at com.sun.javafx.application.LauncherImpl$$Lambda.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(Preloader.java)
at my.app.Preloader$$Lambda.accept(Unknown Source)
at java.util.Optional.ifPresent(Optional.java)
at my.app.Preloader.start(Preloader.java)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication11(LauncherImpl.java)
at com.sun.javafx.application.LauncherImpl$$Lambda.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.java)
at com.sun.javafx.application.PlatformImpl$$Lambda.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java)
at com.sun.javafx.application.PlatformImpl$$Lambda.run(Unknown Source)
at java.security.AccessController.doPrivileged(AccessController.java)
at com.sun.javafx.application.PlatformImpl.lambda$runLater5(PlatformImpl.java)
at com.sun.javafx.application.PlatformImpl$$Lambda.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(this::onOrientationChange); });
(它在 Android 上运行,所以我可以选择检查平台,并且只在非 IOS 左右的平台上运行)
我可以重现你的NPE。
错误发生在您 'eagerly' 调用 service.getOrientation()
时。
如果你看一下code,orientationText
没有初始化,只有当服务初始化和观察者启动时,才会从[=32中检索值=] 原生图层并设置 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
添加一个初始值。