简体   繁体   中英

The activity 'MainActivity' is not declared in AndroidManifest.xml although it is there

It was working all fine till yesterday but today when I tried to run the application, it said "Error running app: Default Activity not found"

  1. Tried solving it by specifying the activity path in the 'edit configuration' option but then it says

    The activity 'MainActivity' is not declared in AndroidManifest.xml

  2. Tried File->Invalidate Caches / Restart but didn't work

  3. Thoroughly went through the manifest to look for syntax error but apparently there are no such mistakes in the manifest

  4. Made sure that the versions are same in the build.gradle file

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

MainActivity.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 re-installing Android Studio 3.4 but still it doesn't work

Sounds like an Android Studio bug. I have run into it myself couple of times. Delete caches and restart the studio.Find more references here :' https://stackoverflow.com/a/54321295/5182150

I really don't know why, but most of the times I do the following:

  1. Open the manifest
  2. Open the manifest merger tab as shown here在此处输入图片说明

At the first moment, you should see everything empty on the left, and some error on the right. At this point, some of the times, for some esoteric reason, the problem disappears. If not, on the right you will have some meaningful error

在 build.gradle(module) 文件中添加代码

sourceSets { main.java.srcDirs += 'src/main/<YOUR DIRECTORY>' }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM