欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android studio报: java.lang.ExceptionInInitializerError 错误

程序员文章站 2023-12-05 20:40:16
一、问题描述 android studio导入一个项目报一堆错误: process: xhs.com.xhswelcomeanim, pid: 1416...

一、问题描述

android studio导入一个项目报一堆错误:

process: xhs.com.xhswelcomeanim, pid: 1416
                                   java.lang.exceptionininitializererror
                                     at com.werb.gankwithzhihu.ui.fragment.zhihufragment.createpresenter(zhihufragment.java:33)
                                     at com.werb.gankwithzhihu.ui.fragment.zhihufragment.createpresenter(zhihufragment.java:25)
                                     at com.werb.gankwithzhihu.ui.base.mvpbasefragment.oncreate(mvpbasefragment.java:35)
                                     at android.support.v4.app.fragment.performcreate(fragment.java:2068)
                                     at android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1055)
                                     at android.support.v4.app.backstackrecord.setlastin(backstackrecord.java:838)
                                     at android.support.v4.app.backstackrecord.calculatefragments(backstackrecord.java:861)
                                     at android.support.v4.app.backstackrecord.run(backstackrecord.java:719)
                                     at android.support.v4.app.fragmentmanagerimpl.execsingleaction(fragmentmanager.java:1638)
                                     at android.support.v4.app.backstackrecord.commitnowallowingstateloss(backstackrecord.java:679)
                                     at android.support.v4.app.fragmentpageradapter.finishupdate(fragmentpageradapter.java:143)
                                     at android.support.v4.view.viewpager.populate(viewpager.java:1240)
                                     at android.support.v4.view.viewpager.populate(viewpager.java:1088)
                                     at android.support.v4.view.viewpager.onmeasure(viewpager.java:1614)
                                     at android.view.view.measure(view.java:18026)
                                     at android.view.viewgroup.measurechildwithmargins(viewgroup.java:5702)
                                     at android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1692)
                                     at android.widget.linearlayout.measurevertical(linearlayout.java:760)
                                     at android.widget.linearlayout.onmeasure(linearlayout.java:629)
                                     at android.view.view.measure(view.java:18026)
                                     at android.view.viewgroup.measurechildwithmargins(viewgroup.java:5702)
                                     at android.support.design.widget.coordinatorlayout.onmeasurechild(coordinatorlayout.java:700)
                                     at android.support.design.widget.headerscrollingviewbehavior.onmeasurechild(headerscrollingviewbehavior.java:90)
                                     at android.support.design.widget.appbarlayout$scrollingviewbehavior.onmeasurechild(appbarlayout.java:1364)
                                     at android.support.design.widget.coordinatorlayout.onmeasure(coordinatorlayout.java:765)
                                     at android.view.view.measure(view.java:18026)
                                     at android.view.viewgroup.measurechildwithmargins(viewgroup.java:5702)
                                     at android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1692)
                                     at android.widget.linearlayout.measurehorizontal(linearlayout.java:1244)
                                     at android.widget.linearlayout.onmeasure(linearlayout.java:631)
                                     at android.view.view.measure(view.java:18026)
                                     at android.view.viewgroup.measurechildwithmargins(viewgroup.java:5702)
                                     at android.widget.framelayout.onmeasure(framelayout.java:465)
                                     at android.support.v7.widget.contentframelayout.onmeasure(contentframelayout.java:135)
                                     at android.view.view.measure(view.java:18026)
                                     at android.view.viewgroup.measurechildwithmargins(viewgroup.java:5702)
                                     at android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1692)
                                     at android.widget.linearlayout.measurevertical(linearlayout.java:760)
                                     at android.widget.linearlayout.onmeasure(linearlayout.java:629)
                                     at android.view.view.measure(view.java:18026)
                                     at android.view.viewgroup.measurechildwithmargins(viewgroup.java:5702)
                                     at android.widget.framelayout.onmeasure(framelayout.java:465)
                                     at android.view.view.measure(view.java:18026)
                                     at android.view.viewgroup.measurechildwithmargins(viewgroup.java:5702)
                                     at android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1692)
                                     at android.widget.linearlayout.measurevertical(linearlayout.java:760)
                                     at android.widget.linearlayout.onmeasure(linearlayout.java:629)
                                     at android.view.view.measure(view.java:18026)
                                     at android.view.viewgroup.measurechildwithmargins(viewgroup.java:5702)
                                     at android.widget.framelayout.onmeasure(framelayout.java:465)
                                     at com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2785)
                                     at android.view.view.measure(view.java:18026)
                                     at android.view.viewrootimpl.performmeasure(viewrootimpl.java:2456)
                                     at android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1440)
                                   
at android.view.view

报 java.lang.exceptionininitializererror错误,网上的解析是:

当在静态初始化块中出现了异常的时候,jvm会抛出 java.lang.exceptionininitializererror异常。如果你了解java中的静态变量,你会知道它们是在类加载的时候进行初始化的。如果在这个静态变量初始化的过程中出现了异常,那么就会抛出 java.lang.exceptionininitializererror异常。任何异常都可能会引发这种情况,比如说,java.lang.arrayindexoutofbound或者java.lang.nullpointerexception。java开发人员通常会被这个错误弄晕,他觉得自己并没有定义任何的静态初始化块,为什么还会抛出exceptionininitializererror异常;事实上,java默认会将静态变量的初始化放在一个默认的静态初始化块中,然后按它们在源文件中声明的顺序来进行初始化。

二、解决方法

找了半天没发现有什么错误啊,最后在androidmanifest.xml清单文件中,发现了问题所在:

Android studio报: java.lang.ExceptionInInitializerError 错误

他在这引用了maapp这个类:

Android studio报: java.lang.ExceptionInInitializerError 错误

而maapp继承的是全局的application这个类,所以在初始化时如果没有引用进来的话,就会报错,而我的那个清单文件中并没有引用进来所以报错了.把他加进来就好了:

Android studio报: java.lang.ExceptionInInitializerError 错误

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!