DrawerLayout 在 Kotlin Android 应用程序中返回 null
DrawerLayout returning null in Kotlin Android application
我开始为 Android 个应用学习 Kotlin。这是我的第一个项目,我正在尝试为我的抽屉菜单设置一个切换侦听器,但出现错误,因为它被返回为 null。我用来查找它的 ID 似乎设置正确。
activity_main.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:title="@string/app_name"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_list"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/toolBar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
drawer_menu.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start" xmlns:app="http://schemas.android.com/apk/res-auto">
<include layout="@layout/activity_main"/>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/menu"/>
</androidx.drawerlayout.widget.DrawerLayout>
还有 MainActivity.kt(我设置开关的地方)
class MainActivity : AppCompatActivity() {
private val rvList: RecyclerView by lazy {
findViewById<RecyclerView>(R.id.rv_list)
}
private val adapter = ContactAdapter()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initDrawer()
bindViews()
updateList()
}
private fun initDrawer() {
val drawerLayout = findViewById<View>(R.id.drawerLayout) as DrawerLayout
val toolBar = findViewById<View>(R.id.toolBar) as Toolbar
setSupportActionBar(toolBar)
val toggle = ActionBarDrawerToggle(this, drawerLayout, toolBar, R.string.open_drawer, R.string.close_drawer)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
}
...
}
这是我遇到的错误:
2022-01-19 10:16:29.863 10567-10567/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: curso.android.contactlist, PID: 10567
java.lang.RuntimeException: Unable to start activity ComponentInfo{curso.android.contactlist/curso.android.contactlist.MainActivity}: java.lang.NullPointerException: null cannot be cast to non-null type androidx.drawerlayout.widget.DrawerLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2861)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2943)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1630)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6626)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
Caused by: java.lang.NullPointerException: null cannot be cast to non-null type androidx.drawerlayout.widget.DrawerLayout
at curso.android.contactlist.MainActivity.initDrawer(MainActivity.kt:34)
at curso.android.contactlist.MainActivity.onCreate(MainActivity.kt:27)
at android.app.Activity.performCreate(Activity.java:7032)
at android.app.Activity.performCreate(Activity.java:7023)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1236)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814)
有谁知道为什么返回空值?
我是不是遗漏了什么,或者您的抽屉布局不是 activity_main.xml 的一部分?如果是这种情况,那么您的 findViewById<View>(R.id.drawerLayout)
将 return 为空的原因是合乎逻辑的。
drawer_menu 不在 activity_main 内,而是相反。所以替换
setContentView(R.layout.activity_main)
和
setContentView(R.layout.drawer_menu)
可能还有更多问题,但这是我最初注意到的
我开始为 Android 个应用学习 Kotlin。这是我的第一个项目,我正在尝试为我的抽屉菜单设置一个切换侦听器,但出现错误,因为它被返回为 null。我用来查找它的 ID 似乎设置正确。
activity_main.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:title="@string/app_name"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_list"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/toolBar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
drawer_menu.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start" xmlns:app="http://schemas.android.com/apk/res-auto">
<include layout="@layout/activity_main"/>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/menu"/>
</androidx.drawerlayout.widget.DrawerLayout>
还有 MainActivity.kt(我设置开关的地方)
class MainActivity : AppCompatActivity() {
private val rvList: RecyclerView by lazy {
findViewById<RecyclerView>(R.id.rv_list)
}
private val adapter = ContactAdapter()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initDrawer()
bindViews()
updateList()
}
private fun initDrawer() {
val drawerLayout = findViewById<View>(R.id.drawerLayout) as DrawerLayout
val toolBar = findViewById<View>(R.id.toolBar) as Toolbar
setSupportActionBar(toolBar)
val toggle = ActionBarDrawerToggle(this, drawerLayout, toolBar, R.string.open_drawer, R.string.close_drawer)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
}
...
}
这是我遇到的错误:
2022-01-19 10:16:29.863 10567-10567/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: curso.android.contactlist, PID: 10567
java.lang.RuntimeException: Unable to start activity ComponentInfo{curso.android.contactlist/curso.android.contactlist.MainActivity}: java.lang.NullPointerException: null cannot be cast to non-null type androidx.drawerlayout.widget.DrawerLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2861)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2943)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1630)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6626)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
Caused by: java.lang.NullPointerException: null cannot be cast to non-null type androidx.drawerlayout.widget.DrawerLayout
at curso.android.contactlist.MainActivity.initDrawer(MainActivity.kt:34)
at curso.android.contactlist.MainActivity.onCreate(MainActivity.kt:27)
at android.app.Activity.performCreate(Activity.java:7032)
at android.app.Activity.performCreate(Activity.java:7023)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1236)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814)
有谁知道为什么返回空值?
我是不是遗漏了什么,或者您的抽屉布局不是 activity_main.xml 的一部分?如果是这种情况,那么您的 findViewById<View>(R.id.drawerLayout)
将 return 为空的原因是合乎逻辑的。
drawer_menu 不在 activity_main 内,而是相反。所以替换
setContentView(R.layout.activity_main)
和
setContentView(R.layout.drawer_menu)
可能还有更多问题,但这是我最初注意到的