今天要在Android端实现一个Navigation导航栏,代码需要通过LayoutInflater.from
对BottomNavigationView
进行初始化,但是总是报下面的错误,看了一下错误堆栈报了The style on this component requires your app theme to be Theme.AppCompat (or a descendant)
这个错误
┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────
│ main, com.example.oednetworkexample.components.general.function.tool.LogHelper.e(LogHelper.java:28)
│ com.example.oednetworkexample.components.general.userinterface.base.OEDTestAbsView.performCreate(OEDTestAbsView.java:188)
├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
│ [ToolPanelOEDTestView]: Binary XML file line #36 in oednetwork.tencent.com.oednetworkexample:layout/oedtest_home_panel: Binary XML file line #36 in oednetwork.tencent.com.oednetworkexample:layout/oedtest_home_panel: Error inflating class android.support.design.widget.BottomNavigationView
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────
2020-08-17 20:51:53.138 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: android.view.InflateException: Binary XML file line #36 in oednetwork.tencent.com.oednetworkexample:layout/oedtest_home_panel: Binary XML file line #36 in oednetwork.tencent.com.oednetworkexample:layout/oedtest_home_panel: Error inflating class android.support.design.widget.BottomNavigationView
2020-08-17 20:51:53.138 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: Caused by: android.view.InflateException: Binary XML file line #36 in oednetwork.tencent.com.oednetworkexample:layout/oedtest_home_panel: Error inflating class android.support.design.widget.BottomNavigationView
2020-08-17 20:51:53.138 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: Caused by: java.lang.reflect.InvocationTargetException
2020-08-17 20:51:53.138 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at java.lang.reflect.Constructor.newInstance0(Native Method)
2020-08-17 20:51:53.138 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
2020-08-17 20:51:53.138 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.LayoutInflater.createView(LayoutInflater.java:852)
2020-08-17 20:51:53.138 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1004)
2020-08-17 20:51:53.138 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
2020-08-17 20:51:53.139 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
2020-08-17 20:51:53.139 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
2020-08-17 20:51:53.139 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
2020-08-17 20:51:53.139 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
2020-08-17 20:51:53.139 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.example.oednetworkexample.components.general.userinterface.base.ToolPanelOEDTestView.onCreateView(ToolPanelOEDTestView.kt:67)
2020-08-17 20:51:53.139 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.example.oednetworkexample.components.general.userinterface.base.OEDTestAbsView.performCreate(OEDTestAbsView.java:134)
2020-08-17 20:51:53.139 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.example.oednetworkexample.components.floatwindow.SystemOEDTestViewManager.attach(SystemOEDTestViewManager.java:57)
2020-08-17 20:51:53.139 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.example.oednetworkexample.components.floatwindow.OEDTestViewManager.attach(OEDTestViewManager.java:57)
2020-08-17 20:51:53.139 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.example.oednetworkexample.components.floatwindow.OEDTestViewManager.attachToolPanel(OEDTestViewManager.java:68)
2020-08-17 20:51:53.140 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.example.oednetworkexample.oedtest.publicheader.OEDTestTool.showToolPanel(OEDTestTool.kt:80)
2020-08-17 20:51:53.140 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.example.oednetworkexample.oedtest.publicheader.OEDTest.showToolPanel(OEDTest.kt:36)
2020-08-17 20:51:53.140 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.example.oednetworkexample.oedtest.userinterface.MainIconOEDTestView$1.onClick(MainIconOEDTestView.java:38)
2020-08-17 20:51:53.140 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.View.performClick(View.java:7448)
2020-08-17 20:51:53.140 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.example.oednetworkexample.components.general.userinterface.base.TouchProxy.onTouchEvent(TouchProxy.java:72)
2020-08-17 20:51:53.140 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.example.oednetworkexample.components.general.userinterface.base.OEDTestAbsView$3.onTouch(OEDTestAbsView.java:142)
2020-08-17 20:51:53.141 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.View.dispatchTouchEvent(View.java:14297)
2020-08-17 20:51:53.141 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3041)
2020-08-17 20:51:53.141 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2742)
2020-08-17 20:51:53.141 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.View.dispatchPointerEvent(View.java:14560)
2020-08-17 20:51:53.141 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5991)
2020-08-17 20:51:53.141 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5794)
2020-08-17 20:51:53.141 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5285)
2020-08-17 20:51:53.141 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5342)
2020-08-17 20:51:53.141 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5308)
2020-08-17 20:51:53.141 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5460)
2020-08-17 20:51:53.141 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5316)
2020-08-17 20:51:53.141 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5517)
2020-08-17 20:51:53.142 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5289)
2020-08-17 20:51:53.142 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5342)
2020-08-17 20:51:53.142 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5308)
2020-08-17 20:51:53.142 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5316)
2020-08-17 20:51:53.142 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5289)
2020-08-17 20:51:53.142 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8055)
2020-08-17 20:51:53.142 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8006)
2020-08-17 20:51:53.142 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7967)
2020-08-17 20:51:53.142 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8177)
2020-08-17 20:51:53.142 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:220)
2020-08-17 20:51:53.142 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.os.MessageQueue.nativePollOnce(Native Method)
2020-08-17 20:51:53.142 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.os.MessageQueue.next(MessageQueue.java:335)
2020-08-17 20:51:53.142 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.os.Looper.loop(Looper.java:183)
2020-08-17 20:51:53.143 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7656)
2020-08-17 20:51:53.143 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2020-08-17 20:51:53.143 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
2020-08-17 20:51:53.143 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2020-08-17 20:51:53.143 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant).
2020-08-17 20:51:53.143 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.support.design.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:221)
2020-08-17 20:51:53.143 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.support.design.internal.ThemeEnforcement.checkAppCompatTheme(ThemeEnforcement.java:196)
2020-08-17 20:51:53.143 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.support.design.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:131)
2020-08-17 20:51:53.144 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.support.design.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:110)
2020-08-17 20:51:53.144 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.support.design.widget.BottomNavigationView.<init>(BottomNavigationView.java:138)
2020-08-17 20:51:53.144 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.support.design.widget.BottomNavigationView.<init>(BottomNavigationView.java:114)
2020-08-17 20:51:53.144 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: ... 49 more
2020-08-17 20:51:53.144 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: java.lang.IllegalArgumentException: Params must be WindowManager.LayoutParams
2020-08-17 20:51:53.144 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:340)
2020-08-17 20:51:53.144 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:109)
2020-08-17 20:51:53.144 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.example.oednetworkexample.components.floatwindow.SystemOEDTestViewManager.attach(SystemOEDTestViewManager.java:59)
2020-08-17 20:51:53.144 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.example.oednetworkexample.components.floatwindow.OEDTestViewManager.attach(OEDTestViewManager.java:57)
2020-08-17 20:51:53.145 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.example.oednetworkexample.components.floatwindow.OEDTestViewManager.attachToolPanel(OEDTestViewManager.java:68)
2020-08-17 20:51:53.145 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.example.oednetworkexample.oedtest.publicheader.OEDTestTool.showToolPanel(OEDTestTool.kt:80)
2020-08-17 20:51:53.145 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.example.oednetworkexample.oedtest.publicheader.OEDTest.showToolPanel(OEDTest.kt:36)
2020-08-17 20:51:53.145 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.example.oednetworkexample.oedtest.userinterface.MainIconOEDTestView$1.onClick(MainIconOEDTestView.java:38)
2020-08-17 20:51:53.145 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.View.performClick(View.java:7448)
2020-08-17 20:51:53.145 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.example.oednetworkexample.components.general.userinterface.base.TouchProxy.onTouchEvent(TouchProxy.java:72)
2020-08-17 20:51:53.145 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.example.oednetworkexample.components.general.userinterface.base.OEDTestAbsView$3.onTouch(OEDTestAbsView.java:142)
2020-08-17 20:51:53.145 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.View.dispatchTouchEvent(View.java:14297)
2020-08-17 20:51:53.145 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3041)
2020-08-17 20:51:53.145 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2742)
2020-08-17 20:51:53.145 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.View.dispatchPointerEvent(View.java:14560)
2020-08-17 20:51:53.145 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5991)
2020-08-17 20:51:53.145 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5794)
2020-08-17 20:51:53.145 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5285)
2020-08-17 20:51:53.145 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5342)
2020-08-17 20:51:53.145 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5308)
2020-08-17 20:51:53.146 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5460)
2020-08-17 20:51:53.146 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5316)
2020-08-17 20:51:53.146 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5517)
2020-08-17 20:51:53.146 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5289)
2020-08-17 20:51:53.146 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5342)
2020-08-17 20:51:53.146 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5308)
2020-08-17 20:51:53.146 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5316)
2020-08-17 20:51:53.146 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5289)
2020-08-17 20:51:53.146 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8055)
2020-08-17 20:51:53.146 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8006)
2020-08-17 20:51:53.146 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7967)
2020-08-17 20:51:53.146 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8177)
2020-08-17 20:51:53.146 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:220)
2020-08-17 20:51:53.146 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.os.MessageQueue.nativePollOnce(Native Method)
2020-08-17 20:51:53.147 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.os.MessageQueue.next(MessageQueue.java:335)
2020-08-17 20:51:53.147 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.os.Looper.loop(Looper.java:183)
2020-08-17 20:51:53.147 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7656)
2020-08-17 20:51:53.147 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2020-08-17 20:51:53.148 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
2020-08-17 20:51:53.148 2754-2754/oednetwork.tencent.com.oednetworkexample W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
定位可以发现主要是因为LayoutInflater.from
解析android.support.design.widget.BottomNavigationView
这个组件错误,进入到BottomNavigationView
打断点调试
...
TintTypedArray a = ThemeEnforcement.obtainTintedStyledAttributes(context, attrs, styleable.BottomNavigationView, defStyleAttr, style.Widget_Design_BottomNavigationView, new int[]{styleable.BottomNavigationView_itemTextAppearanceInactive, styleable.BottomNavigationView_itemTextAppearanceActive});
...
进入继续调试
checkCompatibleTheme(context, set, defStyleAttr, defStyleRes);
进入继续调试
checkAppCompatTheme(context);
进入继续调试
checkTheme(context, APPCOMPAT_CHECK_ATTRS, "Theme.AppCompat");
找到了报错的地方
if (!isTheme(context, themeAttributes)) {
throw new IllegalArgumentException("The style on this component requires your app theme to be " + themeName + " (or a descendant).");
}
跟主题没找到有关。
解决方法
在 android.support.design.widget.BottomNavigationView
中要加入android:theme="@style/AppTheme"
即可解决。
一直认为 LayoutInflater.from() 传入应用上下文一劳永逸且安全,在大多数场景下没有问题,可以成功解析,但是对于有 theme 要求的控件,还是要把android:theme
标签加入。
参考文章
1、https://juejin.im/post/6844903987418759175