Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.content.ActivityNotFoundException: Unable to find explicit activity class - how to fix it?

I have a very basic and yet annoying issue. I created new activities and pass the intent to move to them. Yet when I communicate the two: MainMenu and EventCreationActivity, I get the following error:

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

When I run the activities separately they work perfectly. Here is the code of MainMenu

   package com.mysampleapp;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;

//import com.amazon.mysampleapp.R;

public class MainMenu extends AppCompatActivity {
    //public final static String EXTRA_MESSAGE = "com.mysampleapp.MESSAGE";
    final Intent intent = new Intent(this, EventCreationActivity.class);
    Button newEvent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_menu);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        newEvent = (Button) findViewById(R.id.new_event_button);

        newEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(intent);
            }
        });

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }



}

And manifest:

   <application
        android:name=".Application"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".SplashActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
            android:label="@string/app_name">
            <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="@string/title_activity_main" />
        <activity
            android:name=".LogInActivity"
            android:label="@string/title_activity_log_in"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainMenu"
            android:label="@string/title_activity_main_menu"

            android:theme="@style/AppTheme">
            <!--android:parentActivityName=".LogInActivity"-->
            <!--<meta-data-->
                <!--android:name="android.support.PARENT_ACTIVITY"-->
                <!--android:value="com.mysampleapp.LogInActivity" />-->
        </activity>
        <activity
            android:name=".MainDrawerActivity"
            android:label="@string/title_activity_main_drawer"
            android:theme="@style/AppTheme">
            <!--<meta-data-->
                <!--android:name="android.support.PARENT_ACTIVITY"-->
                <!--android:value="com.mysampleapp.MainMenu" />-->
        </activity>
        <activity
            android:name=".EventCreationActivity"
            android:label="@string/title_activity_event_creation"

            android:theme="@style/AppTheme">
            <!--android:parentActivityName=".MainMenu"-->
            <!--<intent-filter>-->
                <!--<action android:name="android.intent.action.MAIN" />-->
                <!--<category android:name="android.intent.category.DEFAULT" />-->
            <!--</intent-filter>-->
            <!--<meta-data-->
                <!--android:name="android.support.PARENT_ACTIVITY"-->
                <!--android:value="com.mysampleapp.MainMenu" />-->
        </activity>

As you see, I commented some stuff in the Manifest file, but it did not help. I also looked other, similar questions and they did not help.

Thanks in advance, John

like image 525
John Leszcz Avatar asked Nov 02 '25 17:11

John Leszcz


2 Answers

Solution is in your question

have you declared this activity in your AndroidManifest.xml?

use full package name with class like this in your Manifest

android:name="com.mysampleapp.MainMenu"
like image 93
Manish Menaria Avatar answered Nov 04 '25 10:11

Manish Menaria


You're instantiating the intent before the activity has gone through its life cycle methods. This means that the Context that you pass in to the Intent constructor is not valid.

Typically, the intent is created right before you use it:

newEvent.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MainMenu.this, EventCreationActivity.class);
        startActivity(intent);
    }
});
like image 30
tachyonflux Avatar answered Nov 04 '25 08:11

tachyonflux



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!