2017-02-06 10 views
1

我想问,如果有人曾经使用胶Char魅力下降插件的IOS屏幕方向的问题?空指针从阅读屏幕方向从胶Char魅力下降

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左右)

+1

鉴于日志给出了非空方向,我不认为'orientationText'为空。你能修改你的事件处理程序吗?现在不要使用'onOrientationChange'。只需打印方向新值:'service.orientationProperty()。addListener((obs,ov,nv) - > System.out.println(“O:”+ nv));'看看它是否失败。 –

+0

我用'onOrientationChange'的内容更新了我的问题(见底部) - 但是,我可以试试。午餐后会给你结果,好吗? – dzim

回答

1

我可以重现你的NPE。

当您急切地打电话给service.getOrientation()时,会发生错误。

如果你看看codeorientationText没有初始化,并且只有当服务初始化和观察者开始,该值从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

+0

对不起,我昨天忘了回答。无论如何:是的 - 我已经按照你在评论中的建议做了,并且删除了明确的'#getOrientation'调用,只依赖于监听器。非常感谢!再次... – dzim

+1

没问题,现在问题已经修复。它可以使用Charm Down快照或在下一个版本之后。 –