简体   繁体   English

活动“MainActivity”未在 AndroidManifest.xml 中声明,尽管它在那里

[英]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活动“MainActivity”未在 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确保 build.gradle 文件中的版本相同

AndroidManifest.xml 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主活动.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尝试卸载并重新安装 Android Studio 3.4 但仍然无法正常工作

Sounds like an Android Studio bug.听起来像是 Android Studio 的错误。 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删除缓存并重新启动工作室。在此处查找更多参考:' 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>' }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以编程方式检查是否在AndroidManifest.xml中声明了Activity - Programmatically check if an Activity is declared in the AndroidManifest.xml Activity 未在 AndroidManifest.xml 中声明,即使它是 - Activity not declared in AndroidManifest.xml even if it is 错误您是否在 AndroidManifest.xml 中声明了此活动? - Error have you declared this activity in your AndroidManifest.xml? 未声明活动“ MainActivity”? (虽然是) - The activity 'MainActivity' is not declared?! (Although it is) 您是否在AndroidManifest.xml中声明了此活动? 登录活动 - Have you declared this activity in your AndroidManifest.xml ? LoginActivity 无法找到明确的活动类,是否已在AndroidManifest.xml中声明了该活动? - Unable to find explicit activity class have you declared this activity in your AndroidManifest.xml? Android Studio:无法找到显式活动 class 但活动已在 AndroidManifest.xml 中声明 - Android Studio: Unable to find explicit activity class but activity already declared in AndroidManifest.xml 屏幕锁定导致无法找到明确的活动类,您是否已在AndroidManifest.xml中声明了该活动? - Screen Locked causing Unable to find explicit activity class have you declared this activity in your AndroidManifest.xml? 无法找到显式活动类 {}; 您是否在 AndroidManifest.xml 中声明了此活动 - Unable to find explicit activity class {}; have you declared this activity in your AndroidManifest.xml 错误:您是否在 AndroidManifest.xml 中声明了此活动? 当试图意图上课时 - Error: have you declared this activity in your AndroidManifest.xml? when trying to intent a class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM