在Android項(xiàng)目中使用studio時(shí)出現(xiàn) java.lang.ExceptionInInitializerError報(bào)錯(cuò)如何解決?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
10多年的龍沙網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整龍沙建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)公司從事“龍沙網(wǎng)站設(shè)計(jì)”,“龍沙網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
一、問(wèn)題描述
Android studio導(dǎo)入一個(gè)項(xiàng)目報(bào)一堆錯(cuò)誤:
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
報(bào) java.lang.ExceptionInInitializerError錯(cuò)誤,網(wǎng)上的解析是:
當(dāng)在靜態(tài)初始化塊中出現(xiàn)了異常的時(shí)候,JVM會(huì)拋出 java.lang.ExceptionInInitializerError異常。如果你了解Java中的靜態(tài)變量,你會(huì)知道它們是在類加載的時(shí)候進(jìn)行初始化的。如果在這個(gè)靜態(tài)變量初始化的過(guò)程中出現(xiàn)了異常,那么就會(huì)拋出 java.lang.ExceptionInInitializerError異常。任何異常都可能會(huì)引發(fā)這種情況,比如說(shuō),java.lang.ArrayIndexOutOfBound或者java.lang.NullPointerException。Java開(kāi)發(fā)人員通常會(huì)被這個(gè)錯(cuò)誤弄暈,他覺(jué)得自己并沒(méi)有定義任何的靜態(tài)初始化塊,為什么還會(huì)拋出ExceptionInInitializerError異常;事實(shí)上,Java默認(rèn)會(huì)將靜態(tài)變量的初始化放在一個(gè)默認(rèn)的靜態(tài)初始化塊中,然后按它們?cè)谠次募新暶鞯捻樞騺?lái)進(jìn)行初始化。
二、解決方法
找了半天沒(méi)發(fā)現(xiàn)有什么錯(cuò)誤啊,最后在AndroidManifest.xml清單文件中,發(fā)現(xiàn)了問(wèn)題所在:
他在這引用了MaApp這個(gè)類:
而MaApp繼承的是全局的application這個(gè)類,所以在初始化時(shí)如果沒(méi)有引用進(jìn)來(lái)的話,就會(huì)報(bào)錯(cuò),而我的那個(gè)清單文件中并沒(méi)有引用進(jìn)來(lái)所以報(bào)錯(cuò)了.把他加進(jìn)來(lái)就好了:
關(guān)于在Android項(xiàng)目中使用studio時(shí)出現(xiàn) java.lang.ExceptionInInitializerError報(bào)錯(cuò)如何解決問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。