Reputation: 521259
I am experiencing a rather subtle and undetectable bug in one of my Android app's activities. Under certain conditions, when I transition the activity from landscape to portrait, or vice-versa, the app crashes. What is particularly difficult and annoying about this bug is that none of the breakpoints in the activity are being hit in debug mode when I do the transition. In addition, logcat does not seem to be much help, and nothing shows up there either.
So I would be looking for some suggestions about what could cause a crash on orientation change. I can post some code, but I obviously can't post everything, and I'm not sure how much help it would be to post a few fragments from the activity.
Upvotes: 0
Views: 2087
Reputation: 521259
I was on the verge of giving up, when something turned up in Logcat after one of the failed tests:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
com.google.android.gms.maps.model.Marker.setIcon
(com.google.android.gms.maps.model.BitmapDescriptor)' on a null object reference
I followed the stack trace to the point in the activity where the null pointer exception happened. In fact, as many of you suggested in comments, there was some state which was not being restored properly. The exact problem was happening in the initialization of a Google Map.
At first glance now, this question seems to be of the "why isn't this code working" type. However, it is not, because both debugging and, most of the time, Logcat, failed to reveal what the problem was.
As a take home message, if you ever encounter bugs in your Android code, you should not rely on debugging alone, because it might not work. Logcat seems to have come through here, but even it seemed to not be reproducible every time.
Upvotes: 0
Reputation: 4848
The lifecycle of an Activity
is such that on orientation change events like onCreate()
and onStop()
(for example) are not called. So, if you have created objects in onCreate()
they might not be initialized (=null
) when your Activity
returns after being rotated. Check your code for objects that might be initialized in onCreate()
.
Also you might want to add a few more try..catch
with Log.e
messages.
To be honest, if your app crashes, there should be an error logged in the logcat.
Upvotes: 1