Android activity keeps crashing on orientation change - android

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.

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.

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.

Related

Android activity navigation bug

I'm having a weird bug that I'm not sure how to debug. I have have an activity set up to show movie trailers and reviews. Everything looks and works fine when the app is initially started unless I navigate away from the app to another app or youtube to watch the trailer. Then when I come back into my app the trailer names and reviews are all out of the original order and just messed up (some are duplicated). However when I click on the the trailer name (that is now incorrect) it takes me to the correct trailer! Also when I come back to the app if I try rotating the phone sideways it won't rotate. Rotation and everything else works perfectly fine before I navigate away from the app and come back. I'm so confused. I've tried different onSaveInstanceState etc. implementations but nothing is working. I don't even know what code snippet to post since I don't know where the error could possibly be occurring.
Yup as David Wasser wrote above the issue was with my adapter and nothing to do with onSaveInstanceState. I was assigning the textview in the incorrect method of the adapter (I was assigning it where I defined my viewholder and not on the onBind method) so it wasn't being handled properly.

Main Activity Relaunched on App Close

I am making an android app on API 15 for a mobile computer application.
In this app I have a background service, a lot of classes inheriting from AsyncTask, and some sql updates and broadcasting to the service on the app pausing.
I have recently discovered an issue where going to a specific screen in my app, hitting the home button, bringing up my app list, and swiping away my app to kill it results in the app closing and then soon restarting itself at the main activity. It should just close normally.
I have tried placing breakpoints everywhere, and even disabling the background service, but i cannot seem to find any indication of what is restarting the app when it is closed.
I just tried getReferrer inside a try catch and my app says it isnt responding and shuts down even though its in a try catch block.
Both problem seem odd to me.
If it means anything, Android studio will still be debugging the app once it has opened again after i close it.
If anyone could shed some light on what could be restarting my app after i close it, it would be very helpful.
EDIT:
I am using Start_Not_Sticky. this and Start_Redeliver_Intent are ideal option from what i have read.
Thank you to Kevin Krumwiede for suggesting grepping all my code for the main activity's name. I had no idea I could go through all of the code in my project within the IDE instead of going one file at a time.
I had previously tried going through all my classes and searching each individually for reference to it with ctrl+f but i never considered looking in one of my activities that i hadn't modified in months for the new issue.
when I used ctrl+shift+f it all came together as i saw the startActivity function inside of OnDestroy(), not sure how i never noticed this issue before.

How come if you click an ad in my game it causes the game to restart?

I just started to experiment with ads for the first time so I am a complete beginner at this stuff. I have the ads running at the right screens which is good but if you click an ad and it takes you out of the game it causes the game to restart. I made my game in libgdx and I use the pause function and what not but it obviously doesn't call that and in fact just causes a full restart which would make a lot of users angry. Anyone else having this issue or I am doing something wrong?
I am using admob by the way.
EDIT
I also just realized that it happens on my nexus 5 and not my nexus 7.
I think it happened to me some while ago with a game that tried to implement fb sign in functionality. The second it got back from the sign it, it restarted.
What might be happening in your case is that when you click on the add, android switches from your LibGDX activity to the admob activity. Once you come back, the onCreate method gets called again, therefore restarting your app from scratch. (A simple way to verify this is just to add a Log call everytime the app on create method is entered, and check the behaviour of your app)
If that is the case, a possible workaround for it is checking that if your game has been already initialized it should not instantiate it again.
The restart means that your app has crashed. That's standard Android behaviour.
I suspect you have wired something up incorrectly. Look at you log. There's a good chance something hasn't been initialised that should have.

IllegalStateException - Fragment support library

I have a serious problem with my App continously crashing on some users devices with the following exception in an Activity onStart method:
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1310)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:541)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:525)
at android.support.v4.app.DialogFragment.show(DialogFragment.java:123)
I cannot reproduce this error locally in my emulator. I have found a few questions regarding this issue on stackoverflow:
here and
here
I have tried the suggested solution, i.e. do not allow empty bundles in onSaveInstance neither in fragments nor activities.
However the problem remains. This is the only Crash report I ever get, and I get it continously.
Does anyone have a working solution?
I have solved this issue by
upgrading my support lib to revision 10
using workaround posted here: http://code.google.com/p/android/issues/detail?id=23096
i.e.. Add a non-ui fragment in onResumeFragments and invoke further transactions in Listener Callback.
Check my answer here for code: "Can not perform this action after onSaveInstanceState" - why am I getting this exception from my activity's onResume method?
Got no more IllegalStateException so far.
I solved this using this workaround, namely put the code into a methods onPostResume() function in the Activity
Read This Article.
And,
This one.
Once the issue is understood, go ahead and change things properly:
Figure out why your transactions are happening outside your onResume state. Why are these things happening when they shouldn't be. Why are you manually adding and removing fragments with regard to the lifecycle of you app rather than when the user requests. You should maybe need one time in the onCreate() or when the user invokes something. But why are you messing with them in onResume() in the first place? Don't do that.
If you change all the .commit() to .commitAllowStateLoss() then the crashes will go away. But, you're better off not doing that. You are better off making sure you never change your fragments except when the app is fully live. Sometimes that means never loading a fragment from the OnActivityResult() but rather flagging to run in your onPostResume(), and avoiding performing UI changes in async threads that wrongly assume you couldn't have killed the activity in the meantime.
If you just want to spackle everything, .commitAllowStateLoss() will do that. If you want to do it right, make sure you don't fiddle with your fragments after things die.
One of the more recent forms of Android circa Oreo I think, changed this to avoid this annoying error. My answer seems a bit vague but it's because the bug is in the paradigm. You entirely can hit that with various code elements and fairly routinely.

Unexpected crashes in android app spinner and orientation change

To preface this question, this is my first app and I have a very basic understanding of android apps.
My first problem is the issue with screen orientation change. I've seen this question asked on here a million times, but I don't really understand the answers and I'm not sure if it is the same situation or not. My app crashes when the screen orientation changes. I am not using ViewFlipper as a lot of the questions mention. My app also crashes when you try to scroll through the options in a spinner. The spinner is populated correctly, and you can open it and select an option, but scrolling or opening it and changing selection multiple times causes a crash. I think my biggest problem here is I am not used to the Eclipse IDE or java (I am a .net developer primarily) and I feel like it leaves me completely in the dark when it crashes. All it ever says is "Source not found". That doesn't help me much.
So I guess my question is: does anyone know what would cause the application to crash in either of these two instances and/or how do I go about debugging the issues when it simply says "source not found"?
I figured out the issues. Sort of anyways.
The spinner crash was caused by a null value in the array I was binding as the source. So that one was my fault.
The orientation crash was caused by... I'm not really sure. But to investigate I inserted android:configChanges="orientation" into the manifest to be able to handle the onConfigurationChanged event. I inserted the code to override the method and the problem stopped completely.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
That is the code necessary to fix the problem in it's entirety. I have no idea what was causing the problem or why that fixed it so if anyone can shed some light please comment!

Categories

Resources