com.google.android.material.bottomnavigation.BottomNavigatio



>我正在使用底部导航视图开发安卓新闻应用程序,但是当我运行该应用程序时,我收到以下异常

Process: yodgorbek.komilov.musobaqayangiliklari, PID: 19590
java.lang.RuntimeException: Unable to start activity ComponentInfo{yodgorbek.komilov.musobaqayangiliklari/yodgorbek.komilov.musobaqayangiliklari.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.bottomnavigation.BottomNavigationView.setOnNavigationItemSelectedListener(com.google.android.material.bottomnavigation.BottomNavigationView$OnNavigationItemSelectedListener)' on a null object reference`

低于 MainActivity.kt 类 MainActivity : AppCompatActivity(( {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val  bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom)

bottomNavigationView.setOnNavigationItemSelectedListener {
var selectedFragment = Fragment()
when (it.itemId) {
R.id.top_headline -> selectedFragment = TopHeadlinesFragment()
R.id.espn_news -> selectedFragment = ESPNFragment()
R.id.bbc_sport -> selectedFragment = BBCSportFragment()
R.id.football_italia -> selectedFragment = FootballItaliaFragment()
}
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.frame_layout, selectedFragment)
transaction.commit()
return@setOnNavigationItemSelectedListener true
}
}

}

以下activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_gravity="bottom"
app:menu="@menu/bottomnews_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout> 

在您的布局中,底部导航视图和框架布局都没有 id。为两者添加 id

相关内容

  • 没有找到相关文章

最新更新