【发布时间】:2022-06-23 18:18:12
【问题描述】:
您好,我是学习 kotlin 并尝试制作应用程序以及遵循教程的新手。但无论我看什么视频,bottom_navigation 和它都会出错。
任何人都可以看看我的代码,并可能帮助我弄清楚为什么会出现错误。感谢您的任何意见:)
主要活动.ktl
package com.example.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.example.myapplication.Fragments.*
import com.example.myapplication.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
val homeFragment = HomeFragment()
val videoFragment = VideoFragment()
val upcomingFragment = UpcomingFragment()
val podcastFragment = PodcastFragment()
val communityFragment = CommunityFragment()
makeCurrentFragment(homeFragment)
bottom_navigation.setOnNavigationItemSelectedListener
when(it.itemId){
R.id.ic_home_icon_white -> makeCurrentFragment(homeFragment)
R.id.ic_video_white -> makeCurrentFragment(videoFragment)
R.id.ic_upcoming_icon -> makeCurrentFragment(upcomingFragment)
R.id.ic_podcast_white -> makeCurrentFragment(podcastFragment)
R.id.ic_community_white-> makeCurrentFragment(communityFragment)
}
}
private fun makeCurrentFragment(fragment: Fragment) =
supportFragmentManager.beginTransaction().apply {
replace(R.id.fl_wrapper, fragment)
commit()
}
}
主 Activity.xml
<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"
android:background="#1E1E1E"
tools:context=".MainActivity">
<FrameLayout
android:id = "@+id/fl_wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_navigation"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id = "@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:itemBackground="@color/design_default_color_primary"
app:itemIconTint="#fff"
app:itemTextColor="#fff"
app:menu="@menu/my_nav" />
</RelativeLayout>```
【问题讨论】:
-
您面临什么样的错误?你能解释一下吗?
标签: android android-studio kotlin uinavigationbar