Android studio报: java.lang.ExceptionInInitializerError 错误

一、问题描述

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清单文件中,发现了问题所在:

他在这引用了MaApp这个类:

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

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

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。