Android:无法实例化应用程序

发布于 2024-11-04 09:27:17 字数 729 浏览 0 评论 0原文

我重命名了我的包,现在我收到这个奇怪的错误:

Unable to instantiate application
app.MyApplication: java.lang.ClassNotFoundException: 
app.MyApplication in loaderdalvik.system.PathClassLoader

MyApplication 类位于 Application/app 中。清单上写着:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  package="Application">
<application 
    android:label="AGG"
    android:name="app.MyApplication"...

我尝试重新启动,干净构建。它不适用于模拟器或真实设备。

到底发生了什么事?

I renamed my package and now I get this strange error:

Unable to instantiate application
app.MyApplication: java.lang.ClassNotFoundException: 
app.MyApplication in loaderdalvik.system.PathClassLoader

The MyApplication class is in Application/app. The Manifest says:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  package="Application">
<application 
    android:label="AGG"
    android:name="app.MyApplication"...

I tried restart, clean built. Is doesn't work on an emulator nor on a real device.

What on Earth is going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

疯到世界奔溃 2024-11-11 09:27:27

对我有用的是将 build.gradle 中的这些行更改

anotationProcessor "androidx.room:room-compiler:$roomVersion"
anotationProcessor "com.google.dagger:hilt-compiler:$hiltVersion"

为:

kapt "androidx.room:room-compiler:$roomVersion"
kapt "com.google.dagger:hilt-compiler:$hiltVersion"

what works for me is changing these lines in build.gradle:

anotationProcessor "androidx.room:room-compiler:$roomVersion"
anotationProcessor "com.google.dagger:hilt-compiler:$hiltVersion"

to:

kapt "androidx.room:room-compiler:$roomVersion"
kapt "com.google.dagger:hilt-compiler:$hiltVersion"
陌若浮生 2024-11-11 09:27:27

简单清理项目并重建。

Simple clean the project and rebuild.

独闯女儿国 2024-11-11 09:27:27

删除这些行对我有用:

compileOptions {
   sourceCompatibility JavaVersion.VERSION_1_8
   targetCompatibility JavaVersion.VERSION_1_8
}

更新:
这个问题背后的原因是我的电脑上没有安装JAVA 8

Removing these lines worked for me:

compileOptions {
   sourceCompatibility JavaVersion.VERSION_1_8
   targetCompatibility JavaVersion.VERSION_1_8
}

Update:
The reason behind this issue is that i have not installed JAVA 8 on my PC

清眉祭 2024-11-11 09:27:27

对我来说,问题是由于班级的访问级别造成的,它必须是公开的

For me the issue was due to access level of the class, it has to be public

呆° 2024-11-11 09:27:27

如果您正在使用任何依赖项注入库或其他修改应用程序启动的第三方库,请确保配置正确。有些框架需要额外的设置才能正常工作。

If you are using any dependency injection libraries or other third-party libraries that modify application startup, make sure the configuration is correct. Some frameworks require extra settings to work properly.

最笨的告白 2024-11-11 09:27:26

对我来说,问题在于即时运行。禁用它解决了这个问题。

如果我找到重新启用并使其工作的解决方案,我会更新。

For me, the issue was with instant-run. Disabling it solved the issue.

Will update if I find a solution for re-enabling and making it work.

何止钟意 2024-11-11 09:27:26

在我的例子中,它抛出:

Caused by: java.lang.IllegalAccessException: java.lang.Class<foo.bar.MyApp> is not accessible from java.lang.Class<android.app.AppComponentFactory>

原因是 Application 类必须使用修饰符 public 进行声明,例如这样做:

public class MyApp extends Application {
}

而不是:

class MyApp extends Application {
}

In my case it throws:

Caused by: java.lang.IllegalAccessException: java.lang.Class<foo.bar.MyApp> is not accessible from java.lang.Class<android.app.AppComponentFactory>

The reason is the Application class must be declared with the modifier public, e.g. do this:

public class MyApp extends Application {
}

instead of:

class MyApp extends Application {
}
鲸落 2024-11-11 09:27:26

这个答案对我有帮助,如果你使用的是 SDK 17+,基本上将你的 lib 重命名为 libs
https://stackoverflow.com/a/10176048/46459

this answer helped me, basically rename your lib to libs if you're using SDK 17+
https://stackoverflow.com/a/10176048/46459

狂之美人 2024-11-11 09:27:26

就我而言,我已将 JAVA 编译器合规级别从 1.7 更改为 1.6,问题得到解决。

In my case, I've change JAVA compiler compliance level from 1.7 to 1.6 and the problem was solved.

无法言说的痛 2024-11-11 09:27:26

确保清单中的所有引用都已更新以反映您的新包名称。

make sure all references in your manifest have been updated to reflect your new package name.

傲性难收 2024-11-11 09:27:26

package="Application" 应该是 package="MyApplication" 吗?

Should package="Application" be package="MyApplication"?

故人的歌 2024-11-11 09:27:26

您需要更正 android:name 属性。 在 Android 中更改应用程序名称后出现 ClassNotFoundException 中提到的相同问题

You need to correct the android:name attribute. Same problem mentioned at ClassNotFoundException after changing app's name in Android

秋凉 2024-11-11 09:27:25

让我们假设,您的项目基础包实际上是 Application,正如您在 manifest 中所述。

如果您的 MyApplication 类位于此包内(该类的包声明为 package Application;),则您的 application 元素>androidManifest.xml 应该看起来像

<application android:name=".MyApplication" [...]

如果这个 MyApplication 类位于 Application.app 包内 (package Application.app;) ,那么在清单中你应该写:

<application android:name=".app.MyApplication" [...]

如果你没有扩展 android.app.Application (你没有 MyApplication extends android.app.Application 类),只是想为您的应用程序设置一个名称,删除它这个属性,因为它告诉编译器有一个应该实例化的 Application 扩展,而不是默认的 android.app .应用程序

最后,如果第一个假设是错误的,并且您出于任何原因更改了 androidManifest 的 manifest 元素中的包声明,请撤消它或将您的类更新为该包中的类。

Let's assume, that your projects base package is really Application, as you've stated it in the manifest.

If your MyApplication class is inside this package (the package declaration of the class is package Application;), then the application element in your androidManifest.xml should look like

<application android:name=".MyApplication" [...]

If this MyApplication class is inside the Application.app package (package Application.app;), then in the manifest you should write:

<application android:name=".app.MyApplication" [...]

If you didn't extend the android.app.Application (you don't have a MyApplication extends android.app.Application class), just wanted to set a name to your application, remove it this attribute, since it says to the compiler that there is an Application extension that should be instantiated instead of the default android.app.Application.

And finally, if the first assumption is wrong, and you've changed for any reason the package declaration in your androidManifest's manifest element, undo it or update your classes to be in that package.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文