Java – Activity ‘MainActivity’ is not declared in AndroidManifest.xml, although it exists

Activity ‘MainActivity’ is not declared in AndroidManifest.xml, although it exists… here is a solution to the problem.

Activity ‘MainActivity’ is not declared in AndroidManifest.xml, although it exists

Everything worked fine before yesterday, but today when I try to run the application, it says “Error running application: Default activity not found”

  1. Try to resolve it by specifying the activity path in the Edit Configuration option, but then it appears

    The activity ‘MainActivity’ is not declared in AndroidManifest.xml

  2. Tried File->Invalidate Caches/Restart without success

  3. Check the list

  4. thoroughly for syntax errors, but apparently there are no such errors in the list

  5. Make sure the version in the build.gradle file is the same

AndroidManifest.xml

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=". MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Edit

The main activity .java

    package com.example.myapplication;

import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
    import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
    import androidx.appcompat.widget.Toolbar;

import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

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

FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                                    .setAction("Action", null).show();
                        }
                    });
                }

@Override
                public boolean onCreateOptionsMenu(Menu menu) {
                     Inflate the menu; this adds items to the action bar if it is present.
                    getMenuInflater().inflate(R.menu.menu_main, menu);
                    return true;
                }

@Override
        public boolean onOptionsItemSelected(MenuItem item) {
             Handle action bar item clicks here. The action bar will
             automatically handle clicks on the Home/Up button, so long
             as you specify a parent activity in AndroidManifest.xml.
                    int id = item.getItemId();

noinspection SimplifiableIfStatement
                    if (id == R.id.action_settings) {
                return true;
            }

return super.onOptionsItemSelected(item);
        }
       }

Tried uninstalling and reinstalling Android Studio 3.4, but it still doesn’t work

Solution

Sounds like a bug with Android Studio. I’ve come across it a few times myself. Delete the cache and restart Studio. Find more citations here:’
https://stackoverflow.com/a/54321295/5182150

Related Problems and Solutions