【问题标题】:findViewById() must not be nullfindViewById() 不能为空
【发布时间】:2021-08-16 06:55:12
【问题描述】:

我正在尝试制作一个导航抽屉。我已经为抽屉制作了布局文件,为视图分配了适当的 ID,然后在 kotlin 文件中,我使用 lateinit 关键字声明了与视图对应的变量。在onCreate() 方法中,我使用findViewById(R.id...) 初始化了这些变量

当我运行代码时,应用程序崩溃,并且 logcat 显示 NullPointerExceptionfindViewById(R.id.drawerLayout) 不能为 null。这是我的参考代码:

class MainActivity : AppCompatActivity() {

lateinit var drawerLayout: DrawerLayout
lateinit var coordinatorLayout: CoordinatorLayout
lateinit var toolbar: Toolbar
lateinit var frameLayout: FrameLayout
lateinit var navigationView: NavigationView

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    drawerLayout = findViewById(R.id.drawerLayout)
    coordinatorLayout = findViewById(R.id.coordinatorLayout)
    toolbar = findViewById(R.id.toolbar)
    frameLayout = findViewById(R.id.frameLayout)
    navigationView = findViewById(R.id.navigationView)
    setUpToolbar()

    val actionBarDrawerToggle = ActionBarDrawerToggle(
        this@MainActivity,
        drawerLayout,
        R.string.open_drawer,
        R.string.close_drawer)

    drawerLayout.addDrawerListener(actionBarDrawerToggle)    
    actionBarDrawerToggle.syncState()
}


fun setUpToolbar(){
    supportActionBar?.title = "Toolbar Title"       
    supportActionBar?.setHomeButtonEnabled(true)
    supportActionBar?.setDisplayHomeAsUpEnabled(true) 


 }
}

这是我的 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawerLayout"
    tools:context=".MainActivity">

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:background="@color/design_default_color_primary_dark"
        android:theme="@style/ThemeOverlay.AppCompat.Dark"/>


    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/menu_drawer"
        android:layout_gravity = "start"/>

</androidx.drawerlayout.widget.DrawerLayout>

我几乎尝试了所有在阳光下的东西,包括

  1. 以这种方式声明变量:var drawerLayout: DrawerLayout? = null,这可以帮助我避免 findViewById 行中的 nullPointerException,但在我必须执行的代码的进一步部分中给了我一个完全不同的地狱级别使用drawerLayout?.addDrawerListener(actionBarDrawerToggle) 将点击监听器添加到切换。我询问了question here on stackoverflow 关于我在点击侦听器中遇到的这个错误,普遍的共识是不要使用这种声明drawerLayout 的方式,以免它可以为空。

  2. 我尝试过以这种方式使用非空断言调用:var drawerLayout: DrawerLayout!!,但它仍然给我同样的错误

  3. 我在布局文件中检查了又检查了我的DrawerLayout 标签的 ID,因为每个人都说异常可能是由于 ID 和变量名不同,但事实并非如此。我什至清理并重建了项目,使缓存失效,仍然没有变化。

我不知道该怎么做。我被这个错误困扰了很长时间,似乎没有任何效果。如果有人能指出我哪里出错了,那就太好了

【问题讨论】:

  • 能否请您也添加您的 XML 代码?
  • @ShobhithYadav 当然
  • activity_main.xml发布的xml吗?
  • @Sdghasemi 是的
  • 您确定要导入的 R 类属于您的应用程序包吗?如果您在运行代码时添加了 drawerLayout = findViewById(R.id.drawerLayout) 行注释会怎样?

标签: android xml android-studio kotlin nullpointerexception


【解决方案1】:

使用 lazy 而不是 lateinit 将帮助您避免“地狱”

而不是 lateinit var drawLayout: DrawerLayout

使用

val drawerLayout: DrawerLayout by lazy{
  findViewById(R.id.drawerLayout)
}

你也不再需要

drawerLayout = findViewById(R.id.drawerLayout)

drawerLayout 将在第一次需要时被初始化

更多info如果你有兴趣。

【讨论】:

  • 我试过了,它给了我这个错误:Caused by: java.lang.NullPointerException: &lt;get-drawerLayout&gt;(...) must not be null,在接下来的两行中,它告诉我错误在val drawerLayout: DrawerLayout by lazy {val actionBarDrawerToggle = ActionBarDrawerToggle(this@MainActivity, drawerLayout, R.string.open_drawer, R.string.close_drawer)行中
  • 哦,我一开始没有注意到,但您缺少 AppBarConfiguration,这可能是问题所在。这是官方文档。 developer.android.com/guide/navigation/…
猜你喜欢
  • 2021-02-01
  • 1970-01-01
  • 2022-01-19
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-27
相关资源
最近更新 更多