You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android应用Toolbar后添加首页内容布局时触发闪退问题

问题描述

清空*content of homepage xml(首页内容布局文件)*内的代码时,应用可以正常启动运行;但重新为该文件添加布局内容后,应用就会出现闪退。
清空content of homepage内代码时应用可正常运行。
以下为相关布局、主题、清单配置代码,用于定位闪退原因及对应修复方案。

布局文件

activity_home_page.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        android:id="@+id/app_bar_home_page"
        layout="@layout/app_bar_home_page"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_home_page"
        app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>

app_bar_homepage.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomePage">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.BjkuApps.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/Theme.BjkuApps.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_home_page"/>

    <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:id="@+id/loginMoreFabIdForHomePage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginEnd="@dimen/fab_margin"
        android:layout_marginBottom="16dp"
        android:text="@string/login_for_more"
        android:backgroundTint="@color/loginForMoreButton"
        android:textColor="@color/white"
        tools:targetApi="lollipop" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

content_of_homepage.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="10"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_home_page">

    <ViewFlipper
        android:id="@+id/viewFlipperID"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3.4"
        android:layout_marginBottom="8dp"/>

    <ScrollView
        android:id="@+id/scrollViewID"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="6.6"
        android:scrollbars="none">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/home_item_recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </ScrollView>

</LinearLayout>

配置文件

theme.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.BjkuApps" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/default_Color_main</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>

    <style name="Theme.BjkuApps.NoActionBar" parent="Theme.BjkuApps">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
    </style>

    <style name="Theme.BjkuApps.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="Theme.BjkuApps.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

AndroidManifest.xml(HomePage Activity配置段)

<activity
    android:name=".HomePage"
    android:exported="false"
    android:label="@string/title_activity_home_page"
    android:theme="@style/Theme.BjkuApps.NoActionBar" />

内容的提问来源于stack exchange,提问作者Shakawat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 07:30:48