[英]How to use viewbinding in kotlin 1.4
In this code, I used findViewById.在这段代码中,我使用了 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.
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?或者如何使用 findViewById 修复此代码?
[https://developer.android.com/topic/libraries/view-binding][1] [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 -> 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
只需在调用
findViewById
之前调用setContentView
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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.