<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout">
<include layout="@layout/main_content"/>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity = "start"
android:id="@+id/navigation_view"
app:headerLayout="@layout/slide_menu_header"
app:menu="@menu/slide_menu">
</android.support.design.widget.NavigationView>
activity_main.xml выглядит следующим образом, и когда я пытаюсь вызвать
navigation_view.setNavigationItemSelectedListener(this@MainActivity::onNavigationItemSelected)
в MainActivity андроид студия всегда говорила мне, что
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.NavigationView.setNavigationItemSelectedListener(android.support.design.widget.NavigationView$OnNavigationItemSelectedListener)' on a null object reference
Понятия не имею об этой ошибке, поскольку я даю навигационному идентификатору идентификатор, который navigation_view
Вот часть кодов MainActivity.kt
class MainActivity : AppCompatActivity(),NavigationView.OnNavigationItemSelectedListener{
external fun stringFromJNI(): String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//hideStatusBar()
navigation_view.setNavigationItemSelectedListener(this@MainActivity::onNavigationItemSelected)
setContentView(R.layout.activity_main)
tb_iv_slide_menu.setOnClickListener { tbIvSlideMenuOnClick() }
}
override fun onNavigationItemSelected(p0: MenuItem): Boolean {
when (p0.itemId){
R.id.slide_menu_info -> {
Toast.makeText(this@MainActivity,"0",Toast.LENGTH_LONG).show()
drawer_layout.closeDrawer(Gravity.START)}
}
return true
}
Спасибо за любую помощь