Crashes are a headache for all parties involved. Mobile teams hate dealing with them and users hate experiencing them. 62% of users uninstall an app if they experience a crash according to a study by DCI. App instability can quickly lead to failure or at least be very costly. To help minimize your app crashes and the time it takes to address them, we’ve compiled the most common Android crashes and how to fix them. (Have an iOS app, too? Here's how to fix the top five iOS crashes.)
java.lang.NullPointerException
Definitely the most common Android crash. Chances are if you’ve ever developed an Android app then you have run into this error. The most common cause behind NullPointerException is for data being referenced after going out of scope or being garbage collected.
java.lang.IllegalStateException
One of the more common Android crashes that you will likely have encountered if you’ve dealt with fragments. The actual error IllegalStateException can occur from a multitude of sources but at the core of it, the reason is mismanagement of Activity states.
This happens when a time-consuming operation is being done in a background thread, a new Fragment has been created in the meantime and was detached to the Activity before the background thread finished. This results in calling a detached Fragment, throwing such exception.
To fix this issue you should cancel the background thread when pausing or stopping the Fragment. Also, you can use the isAdded() method to check whether the fragment is attached and then to getResources() from activity.
java.lang.IndexOutOfBoundsException
IndexOutOfBoundsException is one of the common RunTimeExceptions that you can run into. If you encountered this error it means that an exception was thrown to indicate that an index of some sort usually an array (but could be a string or a vector) is out of range while using for example List.
There are some common ways that this crash will occur. Using a negative index with arrays, charAt, or substring functions. Also if beginIndex is less than 0 or endIndex is greater than the length of the input string or beginIndex is larger than the endIndex. Another reason is when endIndex - beginIndex is less than 0. But probably the most common reason is when the input array/string/vector is empty.
java.lang.IllegalArgumentException
Probably the most varied and general cause of crashes on the list is the IllegalArgumentException. Its definition in the most simple and it is that your argument is illegal. That could mean a lot of this and that’s why the cause behind an IllegalArgumentException is very varied. There are however a couple of common causes.
One of the more common causes is trying to access UI elements directly from a background thread. Also if you are trying to use a recycled bitmap. Another common cause is forgetting to add an Activity to the Manifest. This won't be caught by the compiler since they are RuntimeExceptions.
To help prevent this crash, focus on making sure castings are always correct. A lot of errors won't be caught by the compiler so you need to be smart when referencing resources since many UI methods accept both strings and integers.
java.lang.OutOfMemoryError
Last but not least on our list is one of the more frustrating crashes. Android devices come in all shapes and memory sizes and it can often be the case that you are dealing with limited resources. OutOfMemoryError occurs when the OS needs to free up memory for high-priority operations and that is taken from your app’s heap allocation. Memory management is becoming easier with newer devices having a lot more memory to deal with. However, not all your users will have access to these devices.
As for the error itself, one of the biggest causes of memory leaks leading to OutOfMemoryError is keeping a reference to an object for too long. This can cause your app to use up a lot more memory than it needs to and hitting that OS limit sooner than it should cause the crash. One of the more common culprits is bitmaps since images can be large in size nowadays. So be sure to recycle any object you can whenever it is no longer necessary.
You can request more heap memory from the OS by adding to your manifest file the attribute
However, it's not advised unless absolutely necessary since this is still a soft request and could be denied.
Learn more:
- The Beginner's Guide for Android App Beta Testing
- Key Performance Optimization Strategies for Your Android App
- Why Does Your App Keep Crashing?
- Google Play Beta Testing: Setting Up Effective Beta Tests
Instabug empowers mobile teams to maintain industry-leading apps with mobile-focused, user-centric stability and performance monitoring.
Visit our sandbox or book a demo to see how Instabug can help your app