Java 从胶子向下读取屏幕方向时的空指针

Java 从胶子向下读取屏幕方向时的空指针,java,ios,javafx,javafxports,gluon-mobile,Java,Ios,Javafx,Javafxports,Gluon Mobile,我想问,是否有人在使用Glion Charm Down插件进行IOS屏幕定位时遇到过问题 JFXMobile插件:org.javafxports:JFXMobile插件:1.3.2 Charm版本:com.glionhq:Charm:4.2.0 downConfig { version = '3.1.0' plugins 'display', 'lifecycle', 'statusbar', 'storage', 'orientation' } 当我试着打电话

我想问,是否有人在使用Glion Charm Down插件进行IOS屏幕定位时遇到过问题

JFXMobile插件:
org.javafxports:JFXMobile插件:1.3.2

Charm版本:
com.glionhq: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)
看一看这个问题,我想可能只有一个原因:

@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();
    }
}
所以我想把代码更新成这样就足够了

Services.get(OrientationService.class).ifPresent(service -> {
service.orientationProperty().addListener(this::onOrientationChange); });


(它在安卓系统上工作,所以我可以选择检查平台,只在非IOS系统上做)

我可以复制你的NPE

当您“急切地”调用
service.getOrientation()
时,会发生此错误

如果查看,
orientationText
未初始化,并且只有在服务初始化且观察者启动时,才会从iOS本机层检索该值,并使用
Platform::runLater
设置该值

这意味着需要很短的时间来获取
orientationContext
的初始值

避免NPE的快速解决方案是使用常规的
ChangeListener
而不是
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);
});
private final ChangeListener onOrientationChange=

(ObservalEvalue鉴于日志给出了一个非空的方向,我认为
orientationText
不是空的。您可以修改您的事件处理程序吗?现在不要使用
onOrientationChange
。只需打印出方向新值:
service.orientationProperty().addListener((obs,ov,nv)->System.out.println(“O:+nv));
并查看它是否失败。我更新了我的问题(见底部)关于onOrientationChange的内容-但是,是的,我可以试试。午饭后给你结果,好吗?对不起,我昨天忘了回答。无论如何:是的-我已经按照你在评论中的建议做了,删除了显式的
#getOrientation
呼叫,只依赖于听众。非常感谢!再次…没有公关问题是,这个问题现在已经解决了。它可以通过魅力下降快照或在下一版本之后提供。
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);
});