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.
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:
Read comments about the workaround which launches ADT with Java 6 instead in this bug report for this issue.
This problem will cause new Android projects not to generate an initial Activity, even when that box was checked.
/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.
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:
onCreatein the Activity class file at
PlaceholderFragmentinner class defined within
See this guide for a more detailed set of instructions with images.
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:
You should be able to use Autocompletion (Ctrl + Space) to complete words as you code. If you can't, try the following steps:
Preferences > Java > Editor > Content Assist > Advanced. Select all checkboxes here (especially ones mentioning Java) and click OK.
Preferences > General > Keysand the find the Command "Content Assist" in the list and remap the "Binding" to a different set of keys and click OK.
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.
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:
Project => Cleanto regenerate the
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/menuXML files very carefully. Do the
@stringreferences resolve? Is the
app:. If it is, try switching them to have an
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.
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.
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.
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.
This problem can be fixed by following these steps:
Android Tools => Fix Project Properties
See this stackoverflow post for the full details.
If this error contains information about the
java.lang.ClassNotFoundException then this usually means one the following issues:
For more possibilities, check out this stackoverflow post for the full details.
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.
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
Project => Clean and then either select "Clean all projects" or ensure the broken project is selected in the list. Then hit OK.
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.
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 188.8.131.52 (that's a Google one). Make sure to restart the emulator after making this change to see the effect.
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.
Window => Show View => Other => Android => LogCat
android:onClickhandler referencing a non-existent method)