在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这个类,所以在初始化时如果没有引用进来的话,就会报错,而我的那个清单文件中并没有引用进来所以报错了.把他加进来就好了:
关于在Android项目中使用studio时出现 java.lang.ExceptionInInitializerError报错如何解决问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。