Sunday 11 November 2012

Handling the activity not Launched Problem

While  creating the application we add new Activities and corresponding to that we have to add the reference to the activity in the AndroidManifest.xml file but there is another important step that needs to be taken into account in order to launch or start a new activity on the emulator.

This deals with adding the Intent - which does the binding of the various components and the functionalties  related to them at the run time.  This can be done between the components of the same or different package.

So we need to write the following code in the Manifest file :
<application
.............
.............
<activity
.............
.............

 <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
</activity>
</application>

Without the above line the application does not gets launched on the emulator.

Now let's understand what the above lines mean in effect:


  • Intent-filter - By this we implicitly define that to which intent the component is to be bound at the run time and we filter out the rest undesirable intents.
    • category android:name="android.intent.category.LAUNCHER" : we specify that we want to be able to launch this specific activity.

Handling The android.content.ActivityNotFoundException: Unable to find explicit activity class


ActivityNotFoundException is the most common exception encountered while developing the android application. This can be compared to the class not found exception. We will see why this exception arises and how to deal with it.

Reason 1: Class Reference Not Mentioned in the AndroidManifest.xml  file
All the activities of our package must be mentioned in the manifest file otherwise the debugging of the application can not be done.This is done by adding the reference to the activity within the <activity></activity> tags .
Solution: Add the reference to the class in the manifest file by simply writing :

<activity android:name=".YourActivityName"></activity>

Reason 2: Syntax Error or Wrong Package Name Mentioned while Starting the New Activity
The correct naming of the package is important. Being a syntactical error this is not so dangerous.

Solution: Check the Package Name and write the following code for launching the new Activity

Intent launchNewIntent = new Intent(CurrentActivity.this,NewActivity.class);
    startActivityForResult(launchNewIntent, 0);


 Reason 3: Updates Not Installed: 
sometimes even if you have done all the coding correctly you still encounter this Exception.

Solution : Go to Help--->> Check For Updates---->>Install Updates(if you are prompted to install new updates)---->>Restart eclipse --->>Run your application again