简体   繁体   English

AndroidStudio:无法解析符号MainActivity

[英]AndroidStudio : Cannot resolve symbol MainActivity

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. 这是目录结构的图像。

目录结构的快照

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. 您的IDE似乎将com.example.fovoapp视为简单的目录结构而不是包。 Also looking at your linked image, the little "J" on the java files tells me that also. 另外,查看链接的图像,java文件中的小“J”也告诉我。 When a java file is set as source usually it shows up as a Class "C". 当java文件设置为源时,它通常显示为类“C”。

I could be wrong but make sure you set your src directory as source and that should fix the issue. 我可能是错的,但请确保将src目录设置为源,这应该可以解决问题。

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

Maybe you use the wrong path for the src directory. 也许你使用错误的路径src目录。
It should be in the path : ./yourApp/src/main and not ./yourApp/src/androidTest 它应该在路径: ./yourApp/src/main / ./yourApp/src/androidTest / ./yourApp/src/main而不是./yourApp/src/androidTest
You can move it manually. 您可以手动移动它。

project structure, right click on src folder->Mark directory as-> sources root. 项目结构,右键单击src文件夹 - >标记目录as-> sources root。

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. 错误是项目没有可以查找活动类的有效源文件夹。

In your build.gradle file add the following. build.gradle文件中添加以下内容。

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

set your compatible api level. 设置兼容的api级别。 i also same the face error so i set my api level to 23 in file-> project structure-> app-> flavour -> target sdk version 我也同样面对错误所以我在文件 - >项目结构 - > app-> flavor - >目标sdk版本中将我的api级别设置为23

Clean Caches 清洁缓存

File -> Invalidate Caches / Restart... 文件 - >使缓存无效/重新启动...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM