为什么我的应用程序在更改 AppTheme 时崩溃

Why do my App crashes when the AppTheme is changed

AppTheme<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"> 更改为 <style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">

后,我的应用程序在启动时崩溃

以下是'HomeActivity.kt'

class HomeActivity : BaseActivity() {

    private lateinit var binding: ActivityHomeBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar
        binding = ActivityHomeBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val window = window
        window.statusBarColor = R.drawable.app_gradient_color_background

        binding.navView.background = null

        supportActionBar!!.setBackgroundDrawable(
            ContextCompat.getDrawable(
                this@HomeActivity,
                R.drawable.app_gradient_color_background
            )
        )

        val navView: BottomNavigationView = findViewById(R.id.nav_view)
        val navController = findNavController(R.id.nav_host_fragment)
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.nav_home,
                R.id.nav_orders,
                R.id.nav_cart
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)

        navView.setupWithNavController(navController)
    }

    override fun onBackPressed() {
        doubleBackToExit()
    }
}

以下是'Logcat'

2021-11-23 02:04:20.714 11275-11275/com.abc.xyz E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.abc.xyz, PID: 11275
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.xyz/com.abc.xyz.ui.activities.HomeActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3835)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4011)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2325)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8633)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
 Caused by: java.lang.NullPointerException
    at com.abc.xyz.ui.activities.HomeActivity.onCreate(HomeActivity.kt:47)
    at android.app.Activity.performCreate(Activity.java:8207)
    at android.app.Activity.performCreate(Activity.java:8191)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)

请帮我解决这个错误。

修复:

我在 xml 布局文件中添加了以下内容

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar_home"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@drawable/app_gradient_color_background"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="24dp"/>

以及 'HomeActivity.kt' 的 onCreate 中的以下内容

setSupportActionBar(binding.toolbarHome)

但是最后的结果并不如我所料

1- 我在顶部看不到工具栏 2- BottomNavigationView 的颜色在 Dark mode 为 'ON' 时变为深色,我不希望这种情况发生。我想要它的颜色永远是白色。

以下是'activity_home.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"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar_home"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@drawable/app_gradient_color_background"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="24dp""/>

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottom_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:fabCradleMargin="10dp"
        app:fabCradleRoundedCornerRadius="50dp">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginEnd="16dp"
            android:background="@android:color/transparent"
            app:itemIconTint="@color/bottom_nav_color"
            app:itemTextColor="@color/bottom_nav_color"
            app:menu="@menu/bottom_nav_menu" />

    </com.google.android.material.bottomappbar.BottomAppBar>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_new"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="Show Categories"
        android:scaleType="center"
        app:layout_anchor="@id/bottom_appbar"
        app:maxImageSize="56dp"
        app:srcCompat="@drawable/fab_image"
        app:tint="@null" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="50dp"
        app:defaultNavHost="true"
        app:layout_anchor="@id/bottom_appbar"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

以下是我在创建自己的工具栏之前在“HomeFragment.kt”中的内容。如何让我的新工具栏在 'HomeFragment'?

中可用
   override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.home, menu)
        super.onCreateOptionsMenu(menu, inflater)

编辑:

注意:我刚刚注意到有关菜单图标颜色的问题,'Dark Mode' 开启时图标为白色,关闭时图标为黑色。我希望它永远是白色的。

当我将 fragment 更改为 androidx.fragment.app.FragmentContainerView 时,应用程序崩溃并出现以下错误。错误 com.abc.xyz.ui.activities.HomeActivity.onCreateHomeActivity.kt:62) 指向 val navController = findNavController(R.id.nav_host_fragment)

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.xyz/com.abc.xyz.ui.activities.HomeActivity}: java.lang.IllegalStateException: Activity com.abc.xyz.ui.activities.HomeActivity@2ed3cdc does not have a NavController set on 2131296976
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3835)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4011)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2325)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8633)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
     Caused by: java.lang.IllegalStateException: Activity com.abc.xyz.ui.activities.HomeActivity@2ed3cdc does not have a NavController set on 2131296976
        at androidx.navigation.Navigation.findNavController(Navigation.java:61)
        at androidx.navigation.ActivityKt.findNavController(Activity.kt:30)
        at com.abc.xyz.ui.activities.HomeActivity.onCreate(HomeActivity.kt:62)
        at android.app.Activity.performCreate(Activity.java:8207)
        at android.app.Activity.performCreate(Activity.java:8191)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3808)

我添加了 app:titleTextColor="@color/colorWhite" 来更改标题的颜色,因为当我添加新的自定义工具栏时,它已更改为黑色。但是,我无法更改 toolbar 中菜单项(搜索、设置)和溢出图标的颜色。正如您在下面的两张图片中看到的那样,它是白色的,而当我 运行 应用程序时它们是黑色的。你能告诉我应该在哪里进行更改吗?

'home_menu.xml'

@drawable/ic_search

因此,通过添加自定义工具栏作为 supportActionBar

解决了原始问题

1- I can't see a toolbar at the top

因为它被 fragment 遮挡了(即它在它后面),要解决此问题,请将工具栏和片段添加到 ConstraintLayout 中并调整约束以使它们不重叠:

<?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"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottom_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:fabCradleMargin="10dp"
        app:fabCradleRoundedCornerRadius="50dp">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginEnd="16dp"
            android:background="@android:color/transparent"
            app:itemIconTint="@color/bottom_nav_color"
            app:itemTextColor="@color/bottom_nav_color"
            app:menu="@menu/bottom_nav_menu" />

    </com.google.android.material.bottomappbar.BottomAppBar>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_new"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="Show Categories"
        android:scaleType="center"
        app:layout_anchor="@id/bottom_appbar"
        app:maxImageSize="56dp"
        app:srcCompat="@drawable/fab_image"
        app:tint="@null" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar_home"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@drawable/app_gradient_color_background"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintTop_toBottomOf="@+id/toolbar_home"
            android:layout_marginBottom="50dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/mobile_navigation" />

    </androidx.constraintlayout.widget.ConstraintLayout>


</androidx.coordinatorlayout.widget.CoordinatorLayout>
  • 也不需要工具栏的上边距,所以android:layout_marginTop="24dp"被移除了。

  • 这个app:layout_anchor="@id/bottom_appbar"需要从fragment中删除,它只需要在fab中锚定到底部栏。

  • fragment 更改为 androidx.fragment.app.FragmentContainerView,因为此处的 fragment 已弃用。

2- The colour of the BottomNavigationView is changed to dark when the Dark mode is 'ON', I don't want that to happen. I want the colour of it white always.

如果你有 themes.xml 文件的夜间版本;请删除它;如果没有固定;请分享此文件