简体   繁体   中英

NullPointerException in event handler in Java

In my Android app I have an event handler that gets called from another thread. 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. I cannot see why they would be null as I instantiate the fields earlier and I don't set the fields anywhere else.

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.

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. 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. I found the solution here and here

And thanks for your help guys, even though there wasn't much for you to go by..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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