Android:无法实例化应用程序
我重命名了我的包,现在我收到这个奇怪的错误:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
对我有用的是将 build.gradle 中的这些行更改
为:
what works for me is changing these lines in build.gradle:
to:
简单清理项目并重建。
Simple clean the project and rebuild.
删除这些行对我有用:
更新:
这个问题背后的原因是我的电脑上没有安装JAVA 8
Removing these lines worked for me:
Update:
The reason behind this issue is that i have not installed JAVA 8 on my PC
对我来说,问题是由于班级的访问级别造成的,它必须是公开的
For me the issue was due to access level of the class, it has to be public
如果您正在使用任何依赖项注入库或其他修改应用程序启动的第三方库,请确保配置正确。有些框架需要额外的设置才能正常工作。
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.
对我来说,问题在于即时运行。禁用它解决了这个问题。
如果我找到重新启用并使其工作的解决方案,我会更新。
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.
在我的例子中,它抛出:
原因是 Application 类必须使用修饰符
public
进行声明,例如这样做:而不是:
In my case it throws:
The reason is the Application class must be declared with the modifier
public
, e.g. do this:instead of:
这个答案对我有帮助,如果你使用的是 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
就我而言,我已将 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.
确保清单中的所有引用都已更新以反映您的新包名称。
make sure all references in your manifest have been updated to reflect your new package name.
package="Application"
应该是package="MyApplication"
吗?Should
package="Application"
bepackage="MyApplication"
?您需要更正
android:name
属性。 在 Android 中更改应用程序名称后出现 ClassNotFoundException 中提到的相同问题You need to correct the
android:name
attribute. Same problem mentioned at ClassNotFoundException after changing app's name in Android让我们假设,您的项目基础包实际上是
Application
,正如您在manifest
中所述。如果您的
MyApplication
类位于此包内(该类的包声明为package Application;
),则您的application
元素>androidManifest.xml 应该看起来像如果这个
MyApplication
类位于Application.app
包内 (package Application.app;
) ,那么在清单中你应该写:如果你没有扩展
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 themanifest
.If your
MyApplication
class is inside this package (the package declaration of the class ispackage Application;
), then theapplication
element in yourandroidManifest.xml
should look likeIf this
MyApplication
class is inside theApplication.app
package (package Application.app;
), then in the manifest you should write:If you didn't extend the
android.app.Application
(you don't have aMyApplication 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 anApplication
extension that should be instantiated instead of the defaultandroid.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.