Android 错误类型 3 错误:Activity 类 {} 不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20915266/
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-20 03:40:47  来源:igfitidea点击:

Error type 3 Error: Activity class {} does not exist

androidandroid-studioandroid-manifest

提问by Jacob

I have an IntelliJ Android project, that I successfully imported to Android Studio 0.4.0. It works perfectly if I don't change anything in manifest. However, when I want to change the launcher activity and run, it fails with this error:

我有一个 IntelliJ Android 项目,已成功导入到 Android Studio 0.4.0。如果我不更改清单中的任何内容,它就可以完美运行。但是,当我想更改启动器活动并运行时,它失败并显示以下错误:

Launching application: com.trackingeng/LandingActivity.
DEVICE SHELL COMMAND: am start -D -n "com.trackingeng/LandingActivity"  
    -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Starting: Intent { act=android.intent.action.MAIN  
    cat=[android.intent.category.LAUNCHER] cmp=com.trackingeng/LandingActivity }
Error type 3
Error: Activity class {com.trackingeng/LandingActivity} does not exist.

When I click Sync Project with Gradle files, it outputs:

当我单击Sync Project with Gradle files 时,它输出:

Project Sync
The project 'TrackingEng' is not a Gradle-based project

Run settings: enter image description here

运行设置: 在此处输入图片说明

回答by S1LENT WARRIOR

I faced a similar problem after refactoring.
This is what i did to resolve this issue:

重构后我遇到了类似的问题。
这是我为解决此问题所做的工作:

  1. Cleaned the Project
  2. Deleted the Builddirectory
  3. Restarted Android Studio
  4. Rebuild the Project
  5. Run
  6. Optionally (Go to the files menu on android, click on "Invalidate Caches / Restart..." uninstall the app on your phone and try again)
  1. 清理项目
  2. 删除了Build目录
  3. 重新启动 Android Studio
  4. 重建项目
  5. 可选(转到 android 上的文件菜单,单击“使缓存无效/重新启动...”卸载手机上的应用程序,然后重试)

And everything worked fine!
I think the key is to restart your IDE.

一切正常!
我认为关键是重新启动您的 IDE

Hope this helps you or anyone else!

希望这可以帮助您或其他任何人!

Edit 1:
If the above steps don't work for you, then deleting Gradle cache seems to be a solution, as pointed out by @Yasitha.

编辑 1:
如果上述步骤对您不起作用,那么删除 Gradle 缓存似乎是一种解决方案,正如@Yasitha 所指出的那样。

Edit 2
As suggested by a couple of users in the comments below, sometimes the issue can be resolved by completely removing and reinstalling the app from your device.
Simply type adb uninstall <package>in terminal to completely remove app from the device.

编辑 2
正如下面评论中的几个用户所建议的那样,有时可以通过从您的设备中完全删除并重新安装该应用程序来解决该问题。
只需输入adb uninstall <package>终端即可从设备中完全删除应用程序。

Edit 3
As mentioned in Abhishek's Answer, one should also try deleting the app from your device in case multiple users are set up on your device.
Simply go to Mobile Settings > Apps > [Your App] > More > Uninstall App for All Users

编辑 3
正如Abhishek 的回答中所述,如果在您的设备上设置了多个用户,还应该尝试从您的设备中删除该应用程序。
只需转到 Mobile Settings > Apps > [Your App] > More > Uninstall App for All Users

UPDATE for Android Studio 2.1 and up

Android Studio 2.1 及更高版本的更新

When running Android Studio 2.1 and up you can also encounter this issue when you have the instant run option enabled in your preferences (is enabled by default).

在运行 Android Studio 2.1 及更高版本时,如果您在首选项中启用了即时运行选项(默认情况下启用),您也会遇到此问题。

To disable this option go to the Preferencesoption in the Android Studiotop menu and look for Instant Runso you can uncheck the first checkbox on that screen.

要禁用此选项,请转到顶部菜单Preferences中的Android Studio选项并查找,Instant Run以便取消选中该屏幕上的第一个复选框。

Disable Instant Run in Android Studio 2.1+

在 Android Studio 2.1+ 中禁用 Instant Run

Anyway that fixed it for me. Originally pointed out by user @yusufonder. I discovered this was the issue since gradle install still worked.

无论如何,它为我修好了。最初由用户@yusufonder 指出。我发现这是问题,因为 gradle install 仍然有效。

回答by Nicks

May be helpful for someone:--

可能对某人有帮助:--

Sometimes when testing on a device, the app doesn't uninstall properly. To verify and fix this:

有时在设备上进行测试时,应用程序无法正确卸载。要验证并修复此问题:

  1. Go to Settings.
  2. Go to Apps.
  3. Select your app. (here, you can verify that your app is not uninstalled properly).
  4. Open the overflow menu on the top right and select Uninstall for all users.
  5. Done. Try to install then launch your app again.
  1. 前往设置
  2. 转到应用程序
  3. 选择您的应用。(在这里,您可以验证您的应用程序是否未正确卸载)
  4. 打开右上角的溢出菜单,然后选择为所有用户卸载
  5. 完毕。尝试安装然后再次启动您的应用程序。

回答by Sakiboy

I would face this problem when uninstalling the app via the device (i.e. dragging the app to the "Uninstall" option). But here is the proper way to uninstall:

通过设备卸载应用程序时我会遇到这个问题(即将应用程序拖到“卸载”选项)。但这是卸载的正确方法:

Use the ./gradlew uninstallAllcommand. This will prevent the Error: Activity class {HomeActivity} does not exist.error.

使用./gradlew uninstallAll命令。这将防止Error: Activity class {HomeActivity} does not exist.错误。

Update:

更新:

If you're lazy you can use the abbreviation for this task: ./gradlew uA.

如果你很懒惰,你可以使用这个任务的缩写: ./gradlew uA

Or define aliases for common gradle tasks in your .bash_profile, doing so will save time, typing, and you won't have to remember every command nor worry about typos. I suggest doing this.

或者在您的 .gradle 中为常见的 gradle 任务定义别名.bash_profile,这样做可以节省时间、打字,而且您不必记住每个命令,也不必担心打字错误。我建议这样做。

回答by Ankit

This happens when you do the following

当您执行以下操作时会发生这种情况

  • connect your device/emulator
  • run the app from Android Studio (AS)
  • use/test the app and uninstall it from the device while it is still connected to your computer
  • try to run the app again from AS
  • 连接您的设备/模拟器
  • 从 Android Studio (AS) 运行应用程序
  • 使用/测试应用程序并在它仍然连接到您的计算机时从设备中卸载它
  • 尝试从 AS 再次运行该应用程序

AS thinks you still have the app in your device.

AS 认为您的设备中仍有该应用程序。

tl;dr - To resolve this, you can simply disconnect your device after uninstalling the app and reconnect it.

tl;dr - 要解决此问题,您只需在卸载应用程序后断开设备连接并重新连接即可。

回答by Dipendra Sharma

The App is already installed for Another user. Please try to uninstall the same app for all the user. Then try.

该应用程序已为其他用户安装。请尝试为所有用户卸载相同的应用程序。那就试试吧。

Or you can try after running adb command.

或者您可以在运行 adb 命令后尝试。

adb uninstall PACKAGE_NAME

where PACKAGE_NAME is the full name such as com.example.myapp

其中 PACKAGE_NAME 是全名,例如 com.example.myapp

回答by Yassin Ajdi

I had the same issue, and this is how I fixed it.

我有同样的问题,这就是我解决它的方法。

Go to Gradle > Tasks > Install > UninstallAll

转到Gradle > 任务 > 安装 > UninstallAll

enter image description here

在此处输入图片说明

回答by Blue5hift

I had the same error after renaming/refactoring. What I did was add the applicationIdproperty attribute to my build.gradle file, and set its value to the application package.

重命名/重构后我遇到了同样的错误。我所做的是将applicationIdproperty 属性添加到我的 build.gradle 文件中,并将其值设置为应用程序包。

In build.gradle:

build.gradle 中

android {
defaultConfig {
    applicationId "com.example.myapp"  
  }
}

回答by Josh

Try changing the name of the Activity in your AndroidManifest.xmlfile.

尝试更改AndroidManifest.xml文件中活动的名称。

Right now it says:

现在它说:

<activity android:name="LandingActivity" >

Try either adding a periodto the beginning of the Activity's name:

尝试在活动名称的开头添加一个句点

<activity android:name=".LandingActivity" >

Or adding the package nameto the beginning of the Activity's name:

或者将包名添加到 Activity 名称的开头:

<activity android:name="com.trackingeng.LandingActivity" >

It also may be a problem that your package name has only two components separated by periods (your package name is "com.trackingeng"; a more standard package name would be "com.trackingeng.app")

这也可能是一个问题,你的包名只有两个用句点分隔的部分(你的包名是“com.trackingeng”;更标准的包名是“com.trackingeng.app”)

回答by Abhishek

Follow Steps Below Go to Mobile setting > Apps > Your App > More > Hit Uninstall app for all users.

按照以下步骤转到移动设置 > 应用程序 > 您的应用程序 > 更多 > 为所有用户点击卸载应用程序。

Reason : Because you are having multiple users in your phone and you had uninstalled that app for only one.

原因:因为您的手机中有多个用户,而您只卸载了一个应用程序。

ENJOY:

请享用:

回答by Long Dao

I think another reason that issue happen is that it is not fully deleted for all users on the device.

我认为发生问题的另一个原因是设备上的所有用户都没有完全删除它。

Go to Settings -> Apps - > Your Apps -> Click to the 3 dots on the top right -> Uninstall for all users

转到设置 -> 应用程序 -> 您的应用程序 -> 单击右上角的 3 个点 -> 为所有用户卸载

It works for me. It happen especially you change the icons of the app or messing around with the AndroidManifest.xml file.

这个对我有用。尤其是您更改应用程序的图标或弄乱 AndroidManifest.xml 文件时,会发生这种情况。