簡體   English   中英

如何在 kotlin 1.4 中使用視圖綁定

[英]How to use viewbinding in kotlin 1.4

在這段代碼中,我使用了 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. 如何應用它?

或者如何使用 findViewById 修復此代碼?

[https://developer.android.com/topic/libraries/view-binding][1]

->主活動

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>

只需在調用findViewById之前調用setContentView

setContentView(R.layout.activity_main)
bottom_navigation = findViewById(R.id.bottom_navigation)

像這樣

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM