Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActivityNotFoundException?

I am getting an ActivityNotFoundException in the following code:

Main.java

Intent intent = new Intent();
     intent.setAction("com.test.app.TEST");
     startActivity(intent); // ActivityNotFoundException

Manifest.xml

<activity android:name=".MainActivity" android:theme="@android:style/Theme.Dialog">
        <intent-filter>
            <action android:name="com.test.app.TEST" />
        </intent-filter>
</activity>
like image 487
jpahn Avatar asked Nov 08 '10 05:11

jpahn


6 Answers

I've had this issue too, as perfectly concisely described by jpahn.

the period at the front did not give any help to me.

even with exactly this (a copy of the original question including edits), I would still get ActivityNotFoundException.

Main.java

Intent intent = new Intent();
 intent.setAction("com.test.app.TEST");
 startActivity(intent); // ActivityNotFoundException

Manifest.xml

<activity android:name=".MainActivity" android:theme="@android:style/Theme.Dialog">
    <intent-filter>
        <action android:name="com.test.app.TEST" />
    </intent-filter>
</activity>

This was resolved, after much trial-and-error, by simply adding this to the intent-filter in the manifest:

<category android:name="android.intent.category.DEFAULT" />

So the final manifest file contained:

<activity android:name=".MainActivity" android:theme="@android:style/Theme.Dialog">
    <intent-filter>
        <action android:name="com.test.app.TEST" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
like image 168
Tom Pace Avatar answered Oct 18 '22 06:10

Tom Pace


I got this error after moving an activity class from one package to another. Clean build solved it (Project -> Clean).

like image 34
Conrad Avatar answered Oct 18 '22 06:10

Conrad


Be sure to declare your activity in the manifest.xml within the aplication:

<application>
    <activity android:name=".YourNewActivity"/>
</application>

To start the new Activity:

Intent intent = new Intent(main.this, YourNewActivity.class);
startActivity(intent);

Where main stands for the current activity,

like image 42
BadSkillz Avatar answered Oct 18 '22 07:10

BadSkillz


Add a . (dot) before your activity name in Android Manifest. So it should be android:name=".WordsToSpeakMainActivity"

like image 43
Mudassir Avatar answered Oct 18 '22 06:10

Mudassir


I have some addition to the @Tom Pace answer. The answer is completely right, but to make it more clear:

ActivityNotFoundException occurs because of absence of

<category android:name="android.intent.category.DEFAULT" />

Because when Android OS see this in the manifest file, understands that this activity can receive intent.

The point ActivityNotFoundException thrown is that, when activity(intent-creator-activity) tries to create intent for other activity(intent-receiver-activity), Android OS sees there is intent for receiver activity but receiver activity does not receive anyone. Then Android OS returns null or empty intent to intent-creator-activity. And startActivity throws that exception.

I have found a code from android developers to avoid this exception:

// Verify the original intent will resolve to at least one activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(chooser);
}

Android Developers: Intent Filters

like image 4
rcelik Avatar answered Oct 18 '22 05:10

rcelik


To be safe you can also call your new activity like this:

Intent intent = new Intent();
intent.setClass(this, THECLASSNAME);
startActivity(intent); // 

However, you must add the activity to the androidmanifest - and write a . in front of it, e.g.

<activity android:name=".YOURACTIVITYNAME"></activity>
like image 3
theXs Avatar answered Oct 18 '22 06:10

theXs