jpahn
jpahn

Reputation: 275

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>

Upvotes: 21

Views: 44789

Answers (10)

I found a solution to this problem... I´m using 2 modules in a android studio project, the thing here is that I needed to add the activity to the main manifest file

<activity android:name="com.HeadApp.ARTry.UnityPlayerActivity"
          android:clearTaskOnLaunch="false" android:label="@string/app_name"
          android:screenOrientation="portrait" 
          android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
/>

I had that in the unity activity manifest, I just copied the activity and paste it in the main manifest and that was it, hope it helps, eve been struggling a lot with this for the past 3 weeks

Upvotes: 0

rcelik
rcelik

Reputation: 56

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

Upvotes: 4

Daniel De Le&#243;n
Daniel De Le&#243;n

Reputation: 13649

To launch an activity by a string definition, use:

Intent intent = new Intent();
intent.setComponent(
        new ComponentName("com.app", "com.app.activity.TheActivity"));
startActivity(intent);

Upvotes: 1

Tayyab Hussain
Tayyab Hussain

Reputation: 1838

There two types of intents in android framework, 1-Implicit intents that you are using,

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

just add one line in intent filter

<intent-filter>
        <action android:name="com.test.app.TEST" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

2- Explicit Intents

Intent i=new Intent(CurrentActivity.this,WhereWeWantToGoActivity.class);

startActivity(i);

Upvotes: 4

theXs
theXs

Reputation: 437

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>

Upvotes: 3

W.K.S
W.K.S

Reputation: 10095

At the very top of your AndroidManifest.xml, you'll see the package attribute

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.example"

and then, in the activity tag, you'll see the name attribute:

<activity
            android:name=".Something"

Make sure that the package name and activity name, when joined together, make the full package specification of your Activity i.e.

com.android.example + .Something = com.android.example.Something

Otherwise, you'll get a ActivityNotFoundException.

Upvotes: 0

Conrad
Conrad

Reputation: 91

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

Upvotes: 9

Tom Pace
Tom Pace

Reputation: 2387

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>

Upvotes: 28

BadSkillz
BadSkillz

Reputation: 2003

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,

Upvotes: 8

Mudassir
Mudassir

Reputation: 13174

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

Upvotes: 4

Related Questions