首页 > 解决方案 > 普通 NavigationDrawer 上的 RuntimeException

问题描述

我的代码出错了。我正在尝试构建一个导航抽屉以在我的应用程序的片段之间跳转。我修改了抽屉以适应我对 XML 代码的需要。但在那之后,当我尝试测试该应用程序时,该应用程序已安装但它在启动时崩溃。它显示此错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{zeinfederico.example.tasklead/zeinfederico.example.tasklead.MainActivity}: java.lang.NullPointerException: findViewById(R.id.drawer_layout) must not be null
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3782)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3961)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2386)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8178)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
     Caused by: java.lang.NullPointerException: findViewById(R.id.drawer_layout) must not be null
        at zeinfederico.example.tasklead.MainActivity.onCreate(MainActivity.kt:32)

我很难理解为什么我的抽屉布局可能为空,因为它实际上显示了抽屉。可以肯定的是,我试图让一个安全的电话接线员。问题是问题然后跳转到 NavigationView 等等。通常当我有这种行为时,我知道下游有些东西写得不好。但我在这里找不到任何相关的东西。

自上次更新 a​​ndroid studio 以来,我遇到了一些奇怪的错误。首先,我的捆绑包支持变得不兼容。我在这个问题中详细说明了这个问题:Plugin "Android Bundle Support" is incompatible。然后,我的 android 应用程序分析器出现异常。也许这些问题与我不知道的问题有关。

我要提前感谢您在我的这个问题上提供的帮助和时间;)

为了帮助解决这个问题,这是我的 MainActivity 代码的副本:


import android.os.Bundle
import android.view.Menu
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.navigation.NavigationView
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import androidx.drawerlayout.widget.DrawerLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar

class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.content_main)
        val toolbar: Toolbar? = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)

//        val fab: FloatingActionButton = findViewById(R.id.fab)
//        fab.setOnClickListener { view ->
//            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                    .setAction("Action", null).show()
//        }
        val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
        val navView: NavigationView = findViewById(R.id.nav_view)
        val navController = findNavController(R.id.nav_host_fragment)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        appBarConfiguration = AppBarConfiguration(setOf(
                R.id.nav_profile, R.id.nav_premium, R.id.nav_schedule, R.id.nav_schedule, R.id.nav_tutorial,
        R.id.nav_about, R.id.nav_settings, R.id.nav_procrastination), drawerLayout)
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment)
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }
} ``` 

Here is content_main.xml:

    <?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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidkotlinnullpointerexceptionnavigation-drawer

解决方案


推荐阅读