RoboVM 中真的很奇怪 NullPointerException
Really strange NullPointerException in RoboVM
如果我使用任何 非空 参数调用以下 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.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);
。没有区别。
- 我还尝试将匿名内部 class 移动到它自己的 class 中,它有一个
private final
字段来存储传递给其构造函数的可运行对象。同样的结果。
我是不是漏掉了一些显而易见的东西???
嗯...这解决了它:
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);
应该已经足够了,因为队列无论如何都应该引用操作对象。但我想世界并不总是按照我解释文档的方式运作。
您对 GC 的看法是正确的。在从 Objective-C 端调用操作之前,您的 NSOperation
实例被垃圾收集。当 NSOperationQueue
调用到 Java 端时,将创建一个 NSOperation
匿名 class 的新实例,它没有对 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 class 提供了采用 Runnable
的 addOperation()
方法的一个版本。使用此方法时,RoboVM 将负责保留 Runnable
实例,而 Objective-C 端需要它。对于采用 Runnable
类型的 @Block
注释参数或任何 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 的文档不会帮助您理解此类问题。
如果我使用任何 非空 参数调用以下 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.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);
。没有区别。 - 我还尝试将匿名内部 class 移动到它自己的 class 中,它有一个
private final
字段来存储传递给其构造函数的可运行对象。同样的结果。
我是不是漏掉了一些显而易见的东西???
嗯...这解决了它:
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);
应该已经足够了,因为队列无论如何都应该引用操作对象。但我想世界并不总是按照我解释文档的方式运作。
您对 GC 的看法是正确的。在从 Objective-C 端调用操作之前,您的 NSOperation
实例被垃圾收集。当 NSOperationQueue
调用到 Java 端时,将创建一个 NSOperation
匿名 class 的新实例,它没有对 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 class 提供了采用 Runnable
的 addOperation()
方法的一个版本。使用此方法时,RoboVM 将负责保留 Runnable
实例,而 Objective-C 端需要它。对于采用 Runnable
类型的 @Block
注释参数或任何 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 的文档不会帮助您理解此类问题。