[英]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"直到昨天它都运行良好,但今天当我尝试运行该应用程序时,它说“运行应用程序时出错:未找到默认活动”
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 中声明
Tried File->Invalidate Caches / Restart but didn't work尝试过文件-> 使缓存无效/重新启动但没有用
Thoroughly went through the manifest to look for syntax error but apparently there are no such mistakes in the manifest彻底检查清单以查找语法错误,但显然清单中没有此类错误
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:我真的不知道为什么,但大多数时候我会这样做:
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.