Reputation: 355
I am getting an error which reads The activity 'MainActivity' is not declared in AndroidManifest.xml
What is the problem here?
Upvotes: 14
Views: 55337
Reputation: 1
I got this issue from copy/pasting code from a previous project and forgot to change this line to match the current project name (xxx) package com.example.xxx
Upvotes: 0
Reputation: 29
I got this problem when I was following the Jetpack Compose Basics tutorial. The error occurred because I copy and pasted their code into my file, including:
package com.example.basicscodelab
This was different from the name of my package. This wasn't actually a compile error, only a warning. I had to change it to:
package com.example.myprojectname
Upvotes: 0
Reputation: 13
Had the same problem with a project which I just downloaded the starter code for an app , everything looked fine in the AndroidManifest.xml , I just deleted the com.example.android.(yourappname).MainActivity and wrote .dMainActivity and it worked.
Upvotes: 0
Reputation: 806
this happened for me when my laptop suddenly shut down when working with project. if you see your activity declaration in your manifest, do not open any xml files. they maybe broken and corrupt after opening files. so do these steps:
Upvotes: 3
Reputation: 19
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.YOURPAKAGE">
<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/Theme.YOURPAKAGE">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
NOTE: Replace your AndroidManifest.xml file code with the above code and you just have to change the name of your package at those places where I have mentioned (YOURPAKAGE) hopefully the problem will be solved
Upvotes: 1
Reputation:
This is an odd one. For me I had to open Edit Configurations
(run configuration) and select <no module>
under the Module drop-down. Then reselect my project under the Module drop-down and Apply. It the built and ran just fine.
Upvotes: 0
Reputation: 321
Try to go to File->Invalidate Caches / Restart and choose invalidate and restart ,it worked for me
Upvotes: 13
Reputation: 268
In my case, none of the answers helped. Luckily, I remembered that I added a variable in build.gradle, which I tried to access from MainActivity.
Problem was that this new variable wasn't present in AndroidManifest.xml. Once I added the variable to AndroidManifest.xml, the problem was solved.
Upvotes: 0
Reputation: 3580
For those cases the you recently changed the android:allowBackup
settings, make sure you also set the tools:replace="android:allowBackup"
in case some of your dependency have it declared in them to override it. I wouldn't know this solution until I manually execute Gradle > app > cleanbuild > lintFix
The exact error I received was:
Error: Attribute application@allowBackup value=(false) from AndroidManifest.xml:41:9-36 is also present at [com.kaopiz:kprogresshud:1.2.0] AndroidManifest.xml:12:9-35 value=(true). Suggestion: add 'tools:replace="android:allowBackup"' to element at AndroidManifest.xml:39:5-254:19 to override.
Upvotes: 1
Reputation: 21
Follow these steps:
If the steps above don't work, write:
com.example.(project name).MainActivity
It should work, worked for me.
Upvotes: 1
Reputation: 330
I was working on my project suddenly my laptop restarts i tried lot of methods but i was not able to fix it. But at the end i did it do these steps and let me know it is working or not?
1. File > Invalidate Cache and restart the the android studio
2. Build > Clean Project and then Rebuild project
3. Sync project with gradle files
if it is still showing you error try these steps
1. Create new activity
Remember to clink on checkBox Launcher Activity
Hope this will Fix your problem
In the end go to AndroidManifest.xml and change New activity Default to previous activity
Upvotes: 0
Reputation: 201
It have three ways to resolve
1:- Go File->Invalidate Caches / choose invalidate and restart ,it works
2:-
If you make MainActivity
make sure you have onCreate
method in it and extend with AppCompatActivity
.
3:- And last option sync project with Gradle file
Upvotes: 1
Reputation: 161
All of my XML file were cut in half for some reason, I'm not sure when it happened (I use Git VCS, so maybe that). I had to go through each of them and restore the code.
Upvotes: 1
Reputation: 31
If you see that error occur after upgrading versions of IntelliJ IDEA or Android Studio, or after Generating a new APK, you may need to refresh the IDE's cache.
File -> Invalidate Caches / Restart...
Upvotes: 0
Reputation: 879
I got this error when moving a lot of files, to fix just resync your gradle files.
File->Sync Project with Gradle Files
Upvotes: 20
Reputation: 11
I was getting an error "The activity 'MainActivity' is not declared in AndroidManifest.xml", even though it was correct in the manifest file.
My problem was that when I created the project I had to mark the item "Use AndroidX artifacts".
Upvotes: 0
Reputation: 37404
This can be only two reason,
the bold one, missing extend statement
class MainActvity extends AppCompatActivity
or
you are using a wrong package name to register your activity so just in this case use ALT+Space then studio will show the options itself.
Upvotes: 1
Reputation: 1879
Sometimes when moving or renaming files, you can have broken XML files. My case that the ic_launcher.xml was corrupt. I regenerated that and worked just fine.
Check every XML in your proyect.
Upvotes: 6
Reputation: 355
Thanks everyone for taking the time out to help a beginner. I found out that I had somehow misspelled the Package in MainActivity.java When I corrected it the error was gone. Thank you all.
Upvotes: 0
Reputation: 1
You should try this
<activity
android:name="com.example.stockquote.StockInfoActivity"
android:label="@string/app_name"
/>
For more detail you can check hereActivity declare
Upvotes: 0
Reputation: 515
You are most likely just missing the below from your AndroidManifest
:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
Here is a full example of an AndroidManifest
:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pejner.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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>
</manifest>
Upvotes: 4
Reputation: 49
replace in your AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.justjava">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Upvotes: 0