In this code, I used findViewById. logcat says java.lang.NullPointerException: findViewById(R.id.bottom_navigation) must not be null So, when I looked up, it is recommended to use it as a viewbinding in kotlin 1.4. How to apply it?
or how to fix this code using findViewById?
[https://developer.android.com/topic/libraries/view-binding][1]
->MainActivity
Class MainActivity : AppCompatActivity() , BottomNavigationView.OnNavigationItemSelectedListener{
private lateinit var bottom_navigation : BottomNavigationView
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when(item.itemId){
...
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
bottom_navigation= findViewById(R.id.bottom_navigation)
setContentView(R.layout.activity_main)
bottom_navigation.setOnNavigationItemSelectedListener(this)
}
}
-> activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="35dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/logo_title"/>
</RelativeLayout>
</Toolbar>
<LinearLayout
android:id="@+id/toolbar_division"
android:background="@color/colorDivision"
android:orientation="horizontal"
android:layout_below="@id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="1dp"></LinearLayout>
<FrameLayout
android:id="@+id/main_content"
android:layout_below="@id/toolbar_division"
android:layout_above="@id/nav_division"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
<LinearLayout
android:id="@+id/nav_division"
android:background="@color/colorDivision"
android:orientation="horizontal"
android:layout_above="@+id/bottom_navigation"
android:layout_below="@id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="1dp"></LinearLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_navigation_main">
</com.google.android.material.bottomnavigation.BottomNavigationView>
just call setContentView
before calling findViewById
setContentView(R.layout.activity_main)
bottom_navigation = findViewById(R.id.bottom_navigation)
like this
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater)
binding.bottomNavigation.setOnNavigationItemSelectedListener(this)
setContentView(binding.root)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.