一、问题描述
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这个类,所以在初始化时如果没有引用进来的话,就会报错,而我的那个清单文件中并没有引用进来所以报错了.把他加进来就好了:
您可能感兴趣的文章:Java java.lang.ExceptionInInitializerError 错误如何解决解决出现 java.lang.ExceptionInInitializerError错误问题启动tomcat时 错误: 代理抛出异常 : java.rmi.server.ExportException: Port already in use: 1099的解决办法详解Java异常处理中throw与throws关键字的用法区别Java中避免空指针异常的方法浅谈java中异常抛出后代码是否会继续执行Java编程中使用throw关键字抛出异常的用法简介简单了解Java编程中抛出异常的方法深入探讨JAVA中的异常与错误处理java.lang.ExceptionInInitializerError异常的解决方法