简体   繁体   English

Java事件处理程序中的NullPointerException

[英]NullPointerException in event handler in Java

In my Android app I have an event handler that gets called from another thread. 在我的Android应用中,我有一个从另一个线程调用的事件处理程序。 When the handler is called some of my private fields are null so I get a NullPointerException because I'm trying to access a method from a null object. 调用处理程序时,我的一些私有字段为null,所以我得到了NullPointerException,因为我试图从null对象访问方法。 I cannot see why they would be null as I instantiate the fields earlier and I don't set the fields anywhere else. 我无法看到为什么它们会为null,因为我较早实例化了这些字段,并且没有在其他任何地方设置这些字段。

Can this have something to do with cross threading? 这与交叉线程有关吗? The fields are set in one thread but accessed by another thread..? 这些字段在一个线程中设置,但被另一个线程访问。 No, right? 没有权利? They shouldn't be null.. I'm going crazy here, but it's probably a tiny mistake that I just can't spot right now. 它们不应该为null。我在这里疯了,但这可能是我现在无法发现的小错误。

I guess it's hard to answer when you don't have the code, but I can't post my whole solution here. 我想当您没有代码时很难回答,但是我不能在此处发布整个解决方案。 Maybe someone can point me in the right direction for what to look for..? 也许有人可以为我指出正确的方向..?

I finally found the solution. 我终于找到了解决方案。 I found out that Android is starting my activity twice. 我发现Android两次启动了我的活动。 So apparently in one activity the state of the object was fine, but in the other I had fields that were null. 因此,显然在一个活动中,对象的状态很好,但是在另一个活动中,我的字段为空。

When the orientation of the device changes, Android apparently stops the activity and starts another one. 当设备的方向发生变化时,Android显然会停止该活动并开始另一个活动。 I found the solution here and here 我在这里这里找到了解决方案

And thanks for your help guys, even though there wasn't much for you to go by.. 并且感谢您的帮助,即使您没有太多的收获。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM