Sep 20, 2012

f Comment

Run an Android Sample Project in Eclipse in MINUTES!

Amazon If you are following official Android programming tutorial at http://developer.android.com/training/ chances are you are at a loss as to how to install sample projects in Eclipse such as http://developer.android.com/shareables/training/NetworkUsage.zip.

You see the "Download the sample" link on many chapters to help you learn the content of the corresponding chapter but it does NOT explain how you can try it out!

This article will teach you how to properly install Android sample projects on Eclipse! As a bonus this article will help you fix the nasty java.lang.ClassNotFoundException error!

I am using Eclipse Indigo for Java Developers.
Step 1: Download the Project
Download and unzip the project (e.g. http://developer.android.com/shareables/training/NetworkUsage.zip) anywhere you want on your computer. In this tutorial we'll be using http://developer.android.com/shareables/training/NetworkUsage.zip as an example!

Step 2: Import the Project into Eclipse Workspace
I assume you already have a workspace going in Eclipse. In my situation I have a workspace just for running Android sample projects.
In Java perspective, right click in Package Explorer and go to Import -> Android -> Existing Android Code Into Workspace.

Browse to the root directory of the project you just downloaded and check the check box in Projects. Check the check box 'Copy projects into workspace'. Click Finish.

You MUST check the check box 'Copy projects into workspace'. Otherwise you may get an 'Invalid Description' error dialog.
Now you should see that Eclipse copied the project over to your workspace and changed the root folder name to something like 'com.example.networkusage.NetworkActivity'.

Click on the project root folder in Package Explorer and in the top menu go to Run -> Run. With any luck you can run the project successfully. If not read on.

You can delete the original folder if you want as it has already been copied to Eclipse's workspace.
Troubleshoot
When you run the sample project if you get the following error chances are you didn't set the configurations properly (or the Android tutorial creators rather).

E/AndroidRuntime(19138): FATAL EXCEPTION: main

E/AndroidRuntime(19138): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.networkusage/com.example.networkusage.NetworkActivity}: java.lang.ClassNotFoundException: com.example.networkusage.NetworkActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.android.networkusage-1.apk]

To fix it, open AndroidManifest.xml and in your main activity locate this line:

android:name="com.example.networkusage.NetworkActivity"
For some reason this line is wrong. It should've been the following line:

android:name="com.example.android.networkusage.NetworkActivity"
To match NetworkActivity.java's package name which is 'com.example.android.networkusage'! Correct this line and run the project again!

Better yet change the line to android:name=".NetworkActivity" and Android will use tag manifest's package attribute as the root for resolving the activities' classes. My manifest's package's value is "com.example.android.networkusage" which is correct!
If you have any questions let me know and I will do my best to help you!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael