修改主题移除标题栏后Android Studio开发的App崩溃求助
切换到NoActionBar主题后App启动崩溃的解决方案
当你将主题父类从Theme.MaterialComponents.DayNight.DarkActionBar改为Theme.MaterialComponents.DayNight.NoActionBar时,崩溃的核心原因是布局文件里的android:paddingTop="?attr/actionBarSize"无法被系统解析。NoActionBar主题中不包含actionBarSize这个属性,系统找不到对应资源就会抛出Resources.NotFoundException,直接导致App启动失败。
解决办法
有两种可行方案,根据你的需求选择:
移除或替换布局中的
actionBarSize引用
直接删除布局根节点的android:paddingTop="?attr/actionBarSize",或者替换为固定值(比如0dp);如果后续会自定义Toolbar,也可以换成?attr/toolbarHeight。修改后的布局示例:<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/nav_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="0dp" android:layout_marginEnd="0dp" android:background="?android:attr/windowBackground" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:menu="@menu/bottom_nav_menu" /> <fragment android:id="@+id/nav_host_fragment_activity_main" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="0dp" app:defaultNavHost="true" app:layout_constraintBottom_toTopOf="@id/nav_view" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:navGraph="@navigation/mobile_navigation" /> </androidx.constraintlayout.widget.ConstraintLayout>在NoActionBar主题中显式定义
actionBarSize属性
如果你需要保留顶部padding,可以在themes.xml的NoActionBar主题里添加该属性的定义,让系统能找到对应值:<style name="Theme.YourAppName" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <!-- 其他已有的属性 --> <item name="actionBarSize">?attr/toolbarHeight</item> </style>
验证方法
修改完成后重新编译运行,不管是现有项目还是新建项目,App都能正常启动,不会再出现崩溃问题。
内容的提问来源于stack exchange,提问作者Ethan
相关产品推荐
相关产品推荐

