在Android项目中使用studio时出现java.lang.ExceptionInInitializerError报错如何解决

在Android项目中使用studio时出现 java.lang.ExceptionInInitializerError报错如何解决?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

10多年的龙沙网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整龙沙建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联公司从事“龙沙网站设计”,“龙沙网站推广”以来,每个客户项目都认真落实执行。

一、问题描述

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报错如何解决

关于在Android项目中使用studio时出现 java.lang.ExceptionInInitializerError报错如何解决问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


当前标题:在Android项目中使用studio时出现java.lang.ExceptionInInitializerError报错如何解决
网页链接:http://scyanting.com/article/gjssed.html