AndroidStudio : Cannot resolve symbol MainActivity

笑着哭i 提交于 2019-12-23 07:13:34

问题


I got an error "Cannot resolve symbol MainActivity" on this code.

<activity
        android:name=".MainActivity"          //here
        android:label="@string/app_name"
        android:launchMode="singleTask" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/> 

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data
                android:host="MainActivity"
                android:scheme="callback" />
        </intent-filter>
    </activity>

Needless to say , MainActivity inherits Activity and package name is correct too.

Why?

Thank you

Here's an image of the directory structure.


回答1:


It's possible that your 'src' directory isn't set as a source directory?

Your IDE seems to be seeing your com.example.fovoapp as a simple directory structure instead of a package. Also looking at your linked image, the little "J" on the java files tells me that also. When a java file is set as source usually it shows up as a Class "C".

I could be wrong but make sure you set your src directory as source and that should fix the issue.




回答2:


package name on AndroidManifest.xml file and your classes must be same.

AndroidManifest.xml header:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
   package="com.ahmet.currencies">

Code:

package com.ahmet.currencies;

import ...;


public class MainActivity extends Activity {}



回答3:


Maybe you use the wrong path for the src directory.
It should be in the path : ./yourApp/src/main and not ./yourApp/src/androidTest
You can move it manually.




回答4:


project structure, right click on src folder->Mark directory as-> sources root.

Now your mainactivity.java file with 'j' symbol to change to 'c' symbol.

The error was that the project did not have a valid source folder from where it could look for the activity class.




回答5:


Clean Caches

File -> Invalidate Caches / Restart...



回答6:


In your build.gradle file add the following.

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



回答7:


set your compatible api level. i also same the face error so i set my api level to 23 in file-> project structure-> app-> flavour -> target sdk version



来源:https://stackoverflow.com/questions/25041104/androidstudio-cannot-resolve-symbol-mainactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!