Java AndroidStudio:无法解析符号 MainActivity

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25041104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:29:47  来源:igfitidea点击:

AndroidStudio : Cannot resolve symbol MainActivity

javaandroidandroid-studiomanifest

提问by Yuya Okada

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

我在此代码上收到错误“无法解析符号 MainActivity”。

<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.

不用说,MainActivity继承了Activity,包名也是对的。

Why?

为什么?

Thank you

谢谢

Here's an image of the directory structure.

这是目录结构的图像。

Snapshot of the directory structure

目录结构的快照

回答by Miguel Lavigne

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

您的“src”目录可能没有设置为源目录?

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".

您的 IDE 似乎将您的 com.example.fovoapp 视为一个简单的目录结构而不是一个包。同时查看您链接的图像,java 文件上的小“J”也告诉我。当一个 java 文件被设置为源文件时,它通常显示为一个“C”类。

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

我可能是错的,但请确保将 src 目录设置为源,这应该可以解决问题。

回答by Alexandre Gombert

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

也许您对 src 目录使用了错误的路径。
它应该在路径中:./yourApp/src/main而不是./yourApp/src/androidTest
您可以手动移动它。

回答by Ahmet Arslan

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

AndroidManifest.xml 文件中的包名称和您的类必须相同。

AndroidManifest.xml header:

AndroidManifest.xml 标头:

<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 {}

回答by sdinesh94

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

项目结构,右键src文件夹->将目录标记为->源根。

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

现在,您的 mainactivity.java 文件中的“j”符号更改为“c”符号。

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

错误是该项目没有可以从中查找活动类的有效源文件夹。

回答by Naskov

In your build.gradlefile add the following.

在您的build.gradle文件中添加以下内容。

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

回答by mehak

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

设置兼容的 api 级别。我也遇到了同样的面部错误,所以我在文件-> 项目结构-> 应用程序-> 风味-> 目标 sdk 版本中将我的 api 级别设置为 23

回答by Nitya Kumar

Clean Caches

清理缓存

File -> Invalidate Caches / Restart...