ActivityNotFoundException: Unable to find explicit activity class

3.5k views Asked by At

I went through many similar questions on SO, but none offered a solution that helped me. Worth to mention that it abruptly happened during debugging, right after I managed to launch this activity (in the same debugging session). I didn't change anything significant so I have absolutely no idea how I got this problem.

I get the error when I try to start a new activity:

Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);

Error message:

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.xxx/com.xxx.MainActivity}; have you declared this activity in your AndroidManifest.xml?

Both activities are declared in the same package com.xxx. Manifest (removed irrelevant code):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.xxx">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:name="misc.App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:replace="android:icon">
        <activity
            android:name=".SplashActivity"
            android:theme="@android:style/Theme.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:label=""
            android:theme="@style/Theme.AppCompat.NoActionBar" />
    </application>
</manifest>

I tried stuff like putting the explicit path both in manifest and when creating the Intent, but it didn't help, as well as cleaning the project.

Any idea? Thanks!

EDIT: Here is the activities part of the project structure:

Image

1

There are 1 answers

0
Neria Nachum On BEST ANSWER

I created a clean copy of MainActivity, duplicated the xml and replaced all references to the old activity and it works. The declaration in the manifest is also identical.

I have no idea why this problem occurred, but a deep inspection definitely doesn't worth it, so this workaround is the least time consuming solution.

EDIT: I faced this issue again and found the real problem. There was a piece of code that disables the aforementioned activity, which then throws ActivityNotFoundException even when the app is closed and re-opened. I guess that clearing cache would have revealed the situation as well.

Anyway, I added a temporary code in the calling activity to enable MainActivity and it worked:

PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(new ComponentName(this, MainActivity.class),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

I obviously removed the disabling code, which is not relevant.