1

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

Neria Nachum
  • 1,519
  • 1
  • 20
  • 37
  • Everything looks right as far as your code goes, do you have Instant Run enabled on your Android Studio https://developer.android.com/studio/run/index.html#instant-run? I've had issues that are hard to explain caused by Instant Run. I would disable it and perform a clean build to see if that resolves the issue. – Bobbake4 Dec 20 '16 at 20:03
  • 1
    do you have another library imported in your project which has its own manifest file? – Amit Upadhyay Dec 20 '16 at 20:03
  • @Bobbake4 It was disabled originally. I actually tried to enable it and then disable it again, to no avail. – Neria Nachum Dec 20 '16 at 20:07
  • Can you add a screenshot of how your folder structure looks like in Android studio under Android View..be sure to show the MainActivity..also shouldn't it be .misc.App instead of misc.App? – Bmbariah Dec 20 '16 at 20:08
  • @AmitUpadhyay Yes I have, yet none of them were added recently. – Neria Nachum Dec 20 '16 at 20:08
  • @Bmbariah Added. And nope, App works as expected. – Neria Nachum Dec 20 '16 at 20:16

1 Answers1

0

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.

Neria Nachum
  • 1,519
  • 1
  • 20
  • 37