简体   繁体   English

AndroidManifest.xml 中的“未解析类”错误

[英]"Unresolved class" error in in AndroidManifest.xml

I get an error in my manifest xml file:我的清单 xml 文件中出现错误:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.shaikhaalothman.playsongservice">

    <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <service
            android:name=".PlaySongService"
            android:enabled="true"
            android:exported="true"></service>
    </application>

</manifest>

and it gives me an error at this line:它在这一行给我一个错误:

 android:name=".PlaySongService"

and the error reads:错误如下:

Unresolved class 'PlaySongService'未解决 class 'PlaySongService'

Validates resource reference inside Android XML files.验证 Android XML 文件中的资源引用。

I haven't found anything that helps to resolve my error here on Stackoverflow, and elsewhere.我没有在 Stackoverflow 和其他地方找到任何有助于解决我的错误的信息。 Anyone that knows what I'm doing wrong?任何人都知道我做错了什么?

Put PlaySongService.java and MainActivity.java in the same package, named com.example.shaikhaalothman.playsongservice .PlaySongService.javaMainActivity.java放在同一个包中,命名为com.example.shaikhaalothman.playsongservice It will resolve automatically.它会自动解决。

Though this is an older thread, I thought I'd include my steps to resolve this issue should it help anyone:虽然这是一个较旧的线程,但我想我会包括解决此问题的步骤,如果它对任何人有帮助:

  1. Verify that the line applicationId "com.domain.packagename" in the module build.gradle file has the correct domain name entered.验证模块 build.gradle 文件中的applicationId "com.domain.packagename"行是否输入了正确的域名。
  2. Make sure that that this domain name matches your package name under your java folder.确保此域名与您的 java 文件夹下的包名称匹配。 An easy way to update this is to go to the settings gear in the top right corner of the Project pane and uncheck "Hide empty middle packages" if it isn't, then right-click refactor-rename the folder with your domain name.更新它的一种简单方法是转到“项目”窗格右上角的设置齿轮,如果不是,则取消选中“隐藏空中间包”,然后右键单击重构并使用您的域名重命名文件夹。
  3. Finally, ensure this domain name is also correctly entered in the package tag at the top of your manifest file.最后,确保在清单文件顶部的包标记中也正确输入了此域名。

My issue was that the directory under java had the incorrect package name, so I had to change it to match the gradle and manifest files.我的问题是 java 下的目录的包名不正确,所以我不得不更改它以匹配 gradle 和 manifest 文件。

You have to include a path in your "build.gradle" file.您必须在“build.gradle”文件中包含一个路径。 I had the same problem as shown in the picture in the link: Unresolved class我遇到了与链接中图片所示相同的问题: Unresolved class

Open the "build.gradle" file in your editor and add a path line to the dependencies, as shown below: dependencies { implementation project(path: ':theClassPathGoesHere') }在您的编辑器中打开“build.gradle”文件,并在依赖项中添加一条路径行,如下所示:dependencies { implementation project(path: ':theClassPathGoesHere') }

In my case, none of the solutions above worked because I had already checked that the package names, etc. were correct.就我而言,上述解决方案均无效,因为我已经检查过包名称等是否正确。

So I took the approach of comparing my activities to a new activity generated by Android Studio.因此,我采用了将我的活动与 Android Studio 生成的新活动进行比较的方法。 I'm on Android Studio 3.1.3.我在 Android Studio 3.1.3 上。 I chose File > New Activity > Basic Activity and the first thing I discovered was the package name it defaulted was red.我选择了 File > New Activity > Basic Activity,我发现的第一件事是它默认的包名称是红色的。

I checked, and sure enough my applicationId in my module-level build.gradle file was not equal to the package name of my existing activities.我检查了一下,果然我的模块级 build.gradle 文件中的 applicationId 不等于我现有活动的包名。 I corrected that, but it didn't make the error go away.我更正了,但这并没有使错误消失。

So, I completed the process of generating the Basic Activity to ensure that I didn't get the Unresolved class error.因此,我完成了生成 Basic Activity 的过程,以确保我没有收到Unresolved class错误。 As I expected, the AndroidManifest.xml entry generated by Android Studio did not have the error.正如我所料,Android Studio 生成的 AndroidManifest.xml 条目没有错误。 Meanwhile my existing activities continued to have an error.与此同时,我现有的活动继续出现错误。 So I then compared my existing activities to the generated one.所以我然后将我现有的活动与生成的活动进行了比较。 I discovered that my activities extended the Activity class where as the generated one extended AppCompatActivity .我发现我的活动扩展了Activity类,而生成的活动扩展了AppCompatActivity So I tried changing from Activity to AppCompatActivity .所以我尝试从Activity更改为AppCompatActivity This generated a bunch of errors I didn't want to deal with, so I changed them back from AppCompatActivity to Activity .这产生了一堆我不想处理的错误,所以我将它们从AppCompatActivity改回Activity And suddenly Android Studio didn't warn me about any Unresolved class errors anymore.突然间,Android Studio 不再警告我任何Unresolved class错误。

I'm not sure what's really going on.我不确定到底发生了什么。 I thought maybe something got corrupted in my .idea directory, but wiping that out and reimporting didn't solve the problem.我想也许我的.idea目录中的某些东西已损坏,但将其清除并重新导入并没有解决问题。

In summary, what seems to eliminate the Unresolved class error is: change your activity's java file from extending Activity to extend AppCompatActivity and then change it back to Activity .总之,似乎消除Unresolved class错误的方法是:将活动的 java 文件从扩展Activity更改为扩展AppCompatActivity ,然后将其改回Activity That resolved the errors for me.这为我解决了错误。

在 android:name="____" 的 android manifest 文件中,我的包名是错误的,所以从包中声明的清单文件的顶部复制我的包名:“____”解决了我的错误

使用 Shift+F6 将 AndroidManifest.xml 中的包从“com.example.loa”重命名为“com.example1.loa”……然后将包从“com.example1.loa”反向重命名为“com.example.loa” ...这将解决问题,因为Android Studio会用新名称重建项目,然后用旧名称重建项目...

唯一对我File > Invalidate Caches / RestartFile > Invalidate Caches / Restart

you should check your package within the java activity.您应该在 java 活动中检查您的包。 clear the package name and rewrite it according to your project.清除包名并根据您的项目进行重写。 example- package com.app.gmail示例 - 包 com.app.gmail

For me it resolved for me by changing: @mipmap/logo1.jpeg to @mipmap/ic_launcher back again.对我来说,它通过将@mipmap/logo1.jpeg 更改为@mipmap/ic_launcher 来解决。

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

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