【问题标题】:Really strange NullPointerException in RoboVMRoboVM 中非常奇怪的 NullPointerException
【发布时间】:2015-06-06 22:52:56
【问题描述】:

如果我使用任何 non-null 参数调用以下 RoboVM 方法:

public static void runOnUiThread(final Runnable runnable) {
    System.out.println("Inside runOnUiThread():");
    System.out.println("  Null-check: "+(runnable==null));

    NSOperation operation = new NSOperation() {

        @Override
        public void main() {
            System.out.println("Inside main():");
            System.out.println("  Null-check: "+(runnable==null));
            runnable.run();                 // NullPointerException here?!? How???
            System.out.println("  main() completed");
        }
    };

    NSOperationQueue.getMainQueue().addOperation(operation);        
}

它输出:

Inside runOnUiThread():
  Null-check: false
Inside main():
  Null-check: true
java.lang.NullPointerException
    at RoboVMTools$1.main(RoboVMTools.java)
    at org.robovm.apple.foundation.NSOperation.$cb$main(NSOperation.java)
    at org.robovm.apple.uikit.UIApplication.main(Native Method)
    at org.robovm.apple.uikit.UIApplication.main(UIApplication.java)
    at Main.main(Main.java)

到底发生了什么???更重要的是,我该如何解决?

  • 我尝试在NSOperationQueue... 之前添加operation.addStrongRef(runnable);。没有区别。
  • 我还尝试将匿名内部类移动到它自己的类中,该类有一个private final 字段来存储传递给其构造函数的runnable。结果相同。

我只是错过了一些非常明显的东西吗???

【问题讨论】:

    标签: ios nullpointerexception robovm


    【解决方案1】:

    您对 GC 的看法是正确的。在从 Objective-C 端调用操作之前,您的 NSOperation 实例已被垃圾收集。当NSOperationQueue 调用Java 端时,将创建NSOperation 匿名类的新实例,该实例不引用Runnable 实例,而是null,结果是NullPointerException 被抛出.

    您使用addStrongRef() 解决它的方式是正确的,尽管只有mainQueue.addStrongRef(operation) 和相应的removeStrongRef() 调用就足够了:

    public static void runOnUiThread(final Runnable runnable) {
    
        final NSOperationQueue mainQueue = NSOperationQueue.getMainQueue();
    
        NSOperation operation = new NSOperation() {
    
            @Override
            public void main() {
                runnable.run();
                mainQueue.removeStrongRef(this);
            }
        };
    
        mainQueue.addStrongRef(operation);
        mainQueue.addOperation(operation);      
    }
    

    这将阻止 Java operation 实例(以及任何可从它访问的 Java 对象,如 Runnable)被 GC,直到 Objective-C NSOperationQueue 实例被释放。由于 Objective-C 侧队列是单例的,它不会在应用程序的生命周期内被释放。

    RoboVM NSOperationQueue Java 类提供了addOperation() 方法的一个版本,它采用Runnable。使用此方法时,RoboVM 将负责保留 Runnable 实例,而 Objective-C 方面需要它。对于采用 @Block 类型为 Runnable 或任何 org.robovm.objc.block.VoidBlock*org.robovm.objc.block.Block* 接口的带注释参数的任何方法也是如此。

    使用这个addOperation() 方法,您的代码就变成了:

    public static void runOnUiThread(Runnable runnable) {
        NSOperationQueue.getMainQueue().addOperation(runnable);      
    }
    

    PS。 RoboVM 使用的 GC 与 Apple 垃圾收集器无关,因此 Apple 的文档不会帮助您理解此类问题。

    【讨论】:

    • 这很有意义。谢谢你。 +1 addOperation(Runnable)。我完全错过了! :) 顺便说一句:RoboVM 真是太棒了!我很高兴我不必处理 Objective-C(太糟糕了!)!你们真棒!非常感谢!我生命中最美好的一天将是有一个可以为 Windows Phone 构建应用程序的 RoboVM 版本! ;) 考虑到您已经拥有的基础设施,这会很难吗?或者你已经在努力了吗? :)
    • 我很高兴听到您喜欢 RoboVM!不,我们不支持 Windows Phone。 MS 最近宣布了Project Astoria,它将把 Android 应用程序带到 Windows Phone。一旦完成,将 RoboVM 移植到 Windows Phone 将没有多大意义。虽然这将是一个有趣的挑战...... :-)
    【解决方案2】:

    嗯...这解决了它:

    public static void runOnUiThread(final Runnable runnable) {
    
        final NSOperationQueue mainQueue = NSOperationQueue.getMainQueue();
    
        NSOperation operation = new NSOperation() {
    
            @Override
            public void main() {
                runnable.run();
    
                mainQueue.removeStrongRef(runnable);
                mainQueue.removeStrongRef(this    );
            }
        };
    
        mainQueue.addStrongRef(runnable );
        mainQueue.addStrongRef(operation);
    
        mainQueue.addOperation(operation);      
    }
    

    但是不要问我为什么这是必要的。 Apple 文档说"In garbage-collected applications, the queue strongly references the operation object." 所以,我之前尝试过的operation.addStrongRef(runnable); 应该已经足够了,因为无论如何队列都应该引用操作对象。但我想世界并不总是按照我解释文档的方式运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多