Infographic: 11 Most Common Android Errors and How to Fix Them
After days of toiling away at your latest Android project, you’ve finally reached the point where you’re ready to put all your hard work to the test. You attach your device or launch the Android Virtual Device (AVD) you created especially for this occasion, tell Android Studio to install your APK—and it doesn’t work.
Don’t panic! Simply work your way down this infographic to find out exactly what’s preventing your app from running, and how you can fix it.
Android Errors List: 11 common errors and how to fix them
This exception is thrown when a call to startActivity fails because the Activity cannot be found. Usually, this means you forgot to declare the Activity in your Manifest.
Java allows you to cast variables of one type to another, as long as this casting happens between compatible data types. A ClassCastException indicates that you’ve tried to cast an object to a subclass that it’s not an instance of, for example attempting to cast a String to an Integer. To resolve this error, navigate to the section of your code indicated in the error message, and then check what object(s) are being cast here.
3. Error converting byte to dex
If you’re encountering this error, then try cleaning (Clean > Build Project) and rebuilding your project (Build > Rebuild Project). If this doesn’t work, then check that the package names you’re using in your classes, match the package name in your project’s Manifest.
This error may occur when you attempt to install your project on an Android Virtual Device (AVD) or physical Android device, and the device doesn’t have enough storage available.
If you’re using an AVD, close it and launch the ‘AVD Manager.’ Find the AVD you want to use, then click the ‘Edit’ icon. In the window that appears, select ‘Show Advanced Settings,’ scroll to the ‘Memory and Storage’ section and increase the amount of space available to your AVD. If you’re using a physical Android tablet or smartphone, then it’s time to spring clean your device to free up some space, for example, by uninstalling apps or clearing out some media files.
A NullPointerException occurs when you use a reference that points to ‘no location’ in memory (null) as though you’re referencing an object. For example, maybe you’re trying to call the instance method of a null object, or modifying the slots of null as though it’s an array.
Your application will then attempt to use null where an object is required, resulting in a NullPointerException.
To fix a NullPointerException you’ll generally need to identify the values that are causing the exception, then locate where these values should be set, and set them.
6. R.layout.main Cannot Be Found / Cannot resolve symbol R
There are various issues that can lead to this error, but the solution is usually straightforward: simply clean (Build > Clean) and build your project (Build > Rebuild Project). You might need to repeat this process a few times before the error message stops appearing.
If this quick-fix doesn’t work, then check that you’re referencing all your layout files correctly throughout your project.
If you see this error after moving some Java files or directories around, then you may have confused Android Studio, as the IDE caches information about your project’s structure. To clear Android Studio’s cache and bring it out of its state of confusion, select ‘File > Invalidate Caches / Restart’ and then click the ‘Invalidate and Restart’ button. Clean and rebuild your project.
This is thrown when your app is making memory requests that the system can’t meet.
There are many potential causes of an OutOfMemoryError, but some of the most common include holding onto references for too long, loading large bitmaps, and memory leaks. The fix will vary depending on the cause of your particular OutOfMemoryError, so you’ll need to track down the problem before you can work out the solution. For example, if the error turns out to be related to large, memory-hogging bitmaps, then a possible fix might be to load the bitmap at a reduced scale.
Android Studio comes with various tools that can help you analyze the way your app is using memory, including Memory Monitor, Allocation Tracker, HPROF Viewer, and the Android Device Monitor’s ‘Heap’ tab.
8. Only the original thread that created a view hierarchy can touch its views
This error occurs when you try to update a view from any thread other than the UI thread. To resolve this issue, make sure you perform any actions involving your app’s user interface on the UI thread only.
If you do need to access the UI thread from other threads, then you can use runOnUiThread.
9. Too many field references
This message will be displayed whenever your project includes more than 65536 references, as this is the maximum number of references that can be invoked by a single dex bytecode file.
When it comes to fixing this error, you have two choices: either reduce the number of references in your project or configure your app to use more than one dex file.
This exception is thrown when your application attempts to perform networking operations on its main thread, as long-running operations have a tendency to block the main thread and can result in an Application Not Responding error.
If your project throws a NetworkOnMainThreadException, then you need to move your Internet-related code to a separate thread. Alternatively, you can use AsyncTask or IntentService to wrap your network operations in a worker thread.
11. Activity has leaked window that was originally added here
This error means that you’ve exited an Activity before the dialogue has been dismissed. To resolve this issue, make sure you call dismiss() to close any dialogues before exiting your Activity.
Haven’t spotted your specific error here? Often, the quickest way to understand and resolve an error is to copy/paste your error message into Google and spend some time exploring the search results.
Related tutorials you might be interested in:
- 6 Ways to Supercharge the Android Emulator
- Ready for Android Nougat? Key Features + Tips & Tricks
- 3 Basic Rules Every Android Developer Should Know
- Sending Push Notifications to Android with Firebase
Jessica Thornsby is a technical writer based in Sheffield. She writes about Android, Eclipse, Java, and all things open source. She is the co-author of iWork: The Missing Manual, and the author of Android UI Design.