Troubleshooting Eclipse Issues - meswapnilwagh/android_guides GitHub Wiki
If you are having trouble with Eclipse or the Emulator, check here for common problems and solutions for Android development problems. A few of the most common issues encountered are listed below.
Setting Up Eclipse
Project generation fails with "Errors running builder 'Android Resource Manager' on project"?
When generating a new project in Eclipse, the generation fails:
Here's a workaround that lets you keep Java 7 as the default but run ADT with Java 6 when you have both installed. Find the Eclipse app in <YOUR_ADT_PATH>/eclipse/Eclipse.app
and right click and select "Show Package Contents" and then navigate to /Contents/MacOS/eclipse.ini
in an editor. Before the -vmargs
line, insert these two lines:
-vm
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Commands/java
Read comments about the workaround which launches ADT with Java 6 instead in this bug report for this issue.
Generated Project Missing Activities?
This problem will cause new Android projects not to generate an initial Activity, even when that box was checked. /src
and /res/layout
were empty.
Fixed by updating the Eclipse plugin: Eclipse > Help > Install New Software
and enter the URL http://dl-ssl.google.com/android/eclipse/
into "Work with", make sure "Developer Tools" is checked and hit Finish
. Leave unchecked "Contact all update sites during install to find required software". Now regenerate the project.
See this stackoverflow post and this other one for more details.
Generated Project has Placeholder Fragment
Note: Easiest way to avoid placeholder fragments is to update to latest eclipse and select Empty Activity rather than Blank Activity when generating activities which does not include the fragment placeholder.
In recent versions of Eclipse, a "Blank Activity" is generated with a built-in placeholder fragment which can't be easily disabled. If you want to remove this placeholder fragment from a "Blank Activity" and work directly with the activity, you can follow these steps:
- Copy contents of fragment XML at
res/layout/fragment_main.xml
- Replace entire contents of activity XML with the contents of the fragment XML
- Delete the fragment XML at
res/layout/fragment_main.xml
- Remove the if statement in the
onCreate
in the Activity class file atsrc/com.../MainActivity.java
- Remove the
PlaceholderFragment
inner class defined withinsrc/com.../MainActivity.java
See this guide for a more detailed set of instructions with images.
Getting "java.lang.IllegalStateException: You need to use a Theme.AppCompat theme..."
First, try simply running Project => Clean...
from the top-menu on the project and then re-running the app. If it still doesn't work, it might be the issue explained in the next paragraph.
This happens when the Android project was generated with a minimum SDK of 10 or below. One fix is to simply generate new projects with a minimum SDK of 14 instead. However when generating with a lower minSDK, in order to maintain compatibility with older versions, you'll notice that the activity Java class (i.e src/.../MainActivity.java) extends from AppCompatActivity
rather than the standard Activity
class. When an activity extends from AppCompatActivity
, this requires the app to use a "backwards compatible theme". Easiest fix is to change the theme for the application within the AndroidManifest.xml
such that application:theme
is set to @style/Theme.AppCompat.Light.DarkActionBar
as shown below:
See this issue, this issue or this issue for more details on this issue.
Problem with Autocompletion?
You should be able to use Autocompletion (Ctrl + Space) to complete words as you code. If you can't, try the following steps:
- Try restarting eclipse, quit and re-open. Check to see if autocompletion has returned.
- Open eclipse and go to the following in the menu:
Preferences > Java > Editor > Content Assist > Advanced
. Select all checkboxes here (especially ones mentioning Java) and click OK. - Open eclipse and go to the following in the menu:
Preferences > General > Keys
and the find the Command "Content Assist" in the list and remap the "Binding" to a different set of keys and click OK.
Using Eclipse
Getting "Cannot Be Resolved to a Type" Error
See a red line under a class that should exist (i.e ArrayList, View)? - Hover over the class and select "import" from the list of suggestions or better use "Cmd + Shift + O" to auto-import all missing types.
Getting "R cannot be resolved to a variable" errors?
This means that the project does not have an R
file generated and is usually a sign of an invalid file within res. Try a series of steps to fix:
- Look for errors in "Console" or "Problems" to resolve (usually invalid XML)
- Try running
Project => Clean
to regenerate theR
file - Try closing and relaunching Eclipse
Be sure to check for these possibilities if that does not work, this is an indication there is an issue with your resources (within res
):
- Check the
res/menu
XML files very carefully. Do the@drawable
and@string
references resolve? Is theshowAsAction
specified asapp:
. If it is, try switching them to have anandroid:
prefix. - Check the
res/drawable*
files and make sure they are named appropriately (only lowercase letters, numbers and underscores)
A more comprehensive guide to every possible way this error can crop up can be found in this comprehensive troubleshooting guide.
Getting "R cannot be resolved" or "resource can not be resolved" errors?
This common issue is a beginner gotcha. In certain cases, you will accidentally import android.R
which will shadow the local resources with the built-in Android resources.
The simple fix is to make sure you don't import android.R. Remove that line at the top of your source file. See this StackOverflow post to learn more about this issue. A more comprehensive guide to every possible way this error can crop up can be found in this comprehensive troubleshooting guide.
Getting "Error executing aapt: Return code 138" in "Problems"?
The first culprit of this error is simply an invalid id specified in one of your Android XML files. For example if one of your android layout or menu XML files has an "android:id" specified that is incorrect such as "btn_foo" or "id/btn_foo" instead of the correct "@+id/btn_foo", you may experience this issue for a project.
Another culprit might be having an app:
prefix in your res/menu
files. Check your menu files and verify that if you don't have the support-v7 library included that your menu xml files do not contain app:showAsAction
but instead have android:showAsAction
. Make sure to clean and rebuild once you've made this change.
Getting this error could also mean your Android project compilation is likely corrupted in some way. Typically this error will occur on projects in particular cases such as generating a new icon or adding a new xml file. If you see this error in your "Problems" window after a clean, try closing and reopening Eclipse first, then try doing a Project => Clean
.
Getting "Unable to execute dex: Multiple dex files define" or "java.nio.BufferOverflowException"?
This problem prevents a project from being built, and instead displays the error "Unable to execute dex: Multiple dex files define" when compiling is attempted. The issue is that the bin directory becomes included in the project build path. Excluding the bin from the build path often resolves the problem. Right click on the project name, select Build Path -> Configure Build Path
and in Java Build Path, go to the tab Order and Export
. See the full details of the solution here.
Getting "gen already exists but is not a source folder"?
This problem can be fixed by following these steps:
- Right click on the project and go to "Properties"
- Select "Java Build Path" on the left
- Open the "Source" tab
- Click "Add Folder..."
- Check the "gen" folder and click "Ok" and "Ok" again
- Right click on the project and select
Android Tools => Fix Project Properties
See this stackoverflow post for the full details.
Getting "Unable to instantiate activity ComponentInfo{...}"?
If this error contains information about the java.lang.ClassNotFoundException
then this usually means one the following issues:
- Please verify that every Activity is registered in your
AndroidManifest.xml
- Check that you're Activity is a public class and not protected or private
- Ensure that any dependency jars are checked in the "Java Build Path"
For more possibilities, check out this stackoverflow post for the full details.
Getting "java.lang.ClassCastException: android.widget.Foo cannot be cast to android.widget.Bar"?
There are several possible reasons for this exception. The most logical is that in the code you are literally casting a view to an incorrect type. For example, suppose we defined a layout XML with:
<EditText
android:id="@+id/etNewItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Now suppose in the Activity code we write:
ImageView etNewItem = (ImageView) findViewById(R.id.etNewItem);
This would generate the error above "java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.ImageView". First, check if you have improperly cast a view to the wrong type.
If that doesn't work, and the view appears to be properly cast to the correct type, then the resource mapping may simply be confused. In that case, as explained here, you need to clean your project by running Project => Clean...
from the menu. Cleaning will regenerate the resource mappings and often fix this issue.
Imported Project Won't Compile
If you have imported an existing Android project into the Eclipse workspace and the project gives an error such as "cannot resolve target android-17" or another obscure error, follow this checklist below.
Select Build Target
Right click on the project name in the package explorer on the left side and select "Properties". Select "Android" on the left side and then pick a sensible "Project Build Target" (i.e Android 4.4.2).
Fix Imported Libraries
Right click on the project name in the package explorer on the left side and select "Properties". Select "Android" on the left side and verify there are no "missing" libraries. If there is a missing library project, remove that library and add the library into your project using the "Add" button. Keep in mind you need to have the necessary library projects loaded into eclipse for this to work. (See screenshot above)
Fix Build Path
Right click on the project name in the package explorer on the left side and select "Properties". Select "Java Build Path" on the left side and select the tab "Libraries". Expand all the groups displayed there. Ensure that there are no "missing" libraries or folders on the build path.
Fix Project Properties
Right click on the project name in the package explorer on the left side and select Android Tools => Fix Project Properties
.
Clean the Project
Select Project => Clean
and then either select "Clean all projects" or ensure the broken project is selected in the list. Then hit OK.
Restart Eclipse
If after all these steps and a clean the project is still not able to compile, try restarting eclipse and see if the project is able to compile successfully after a reboot.
Emulator
App running in emulator but no logs showing up in LogCat?
- Switch to DDMS mode in Eclipse
- Verify the emulator is listed and select the emulator
- Select the small down arrow and click "reset adb"
- Still having problems? Time to restart eclipse
Cannot access the internet
In some cases, even after requesting Internet access in the Manifest.xml, you might experience problems when trying to access Internet.
One fix for this is to manually specify DNS in Configuration / Target / Emulator
launch parameters: -dns-server 8.8.8.8
(that's a Google one). Make sure to restart the emulator after making this change to see the effect.
Booting the emulator is slow?
- Open "Window => Android Virtual Device Manager"
- Click "Edit" on your Virtual Device and verify "Intel x86" is selected for CPU
- Make sure not to close your emulator once it is booted, leave it open and just re-run
Started emulator either doesn't boot, freezes computer or looks glitchy
- Go to Intel HAXM page and install the latest hotfixes for your platform.
- Open "Window => Android Virtual Device Manager"
- Click "Edit" on your Virtual Device and toggle the "Use Host GPU" checkbox
- Verify the CPU has "Intel x86" selected rather than "ARM"
- Now fully restart the emulator and relaunch
Emulator is crashing unexpectedly and my mac has dual-monitors
Check out this post for quick fix. In short, we have to edit a file specifying the X/Y coordinates for the virtual device to use to position itself on startup. If these coordinates are outside the normal bounds of the primary monitor, then the emulator will crash.
- Edit the file
~/.android/avd/.avd/emulator-user.ini
- Reset the value of both
window.x
andwindow.y
to 0
Android
I am getting an error "Application has stopped" in a dialog and my app closes
- This means your application had a runtime crash
- If you don't see LogCat, open with
Window => Show View => Other => Android => LogCat
- Use LogCat to debug where the crash originated (or use the debugger)
- Find the error stacktrace and identify the line (in your code) that triggered the error.
- This is often a NullPointerException (accessing a null object) or other null references (such as an
android:onClick
handler referencing a non-existent method)