gpt4 book ai didi

java - 从 Gluon Charm Down 读取屏幕方向的空指针

转载 作者:行者123 更新时间:2023-11-29 11:49:03 29 4
gpt4 key购买 nike

我想问一下,是否有人在使用 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();
}
}

我的猜测是,orientationTextnull,因此它崩溃了。

我猜 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。

错误发生在您“急切地”调用 service.getOrientation() 时.

如果您查看 code , orientationText未初始化,只有在服务初始化和观察者启动时,该值才会从 iOS native 层检索并设置为 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 添加一个初始值.

关于java - 从 Gluon Charm Down 读取屏幕方向的空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42065240/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com