Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使活动在片段内工作?_Android_Xml_Android Fragments_Kotlin - Fatal编程技术网

Android 如何使活动在片段内工作?

Android 如何使活动在片段内工作?,android,xml,android-fragments,kotlin,Android,Xml,Android Fragments,Kotlin,当我从导航抽屉中选择chillzone片段时,我可以切换片段,但当我尝试从BottomNavigationView中选择某个片段时,什么都没有发生 活动\u main.xml chillzone_fragment.xml ChillZoneActivity package com.rishabhdeepsingh.metaapp.ui.chillzone 导入android.os.Bundle 导入android.util.Log 导入com.google.android.materia

当我从导航抽屉中选择chillzone片段时,我可以切换片段,但当我尝试从BottomNavigationView中选择某个片段时,什么都没有发生

活动\u main.xml


chillzone_fragment.xml


ChillZoneActivity

package com.rishabhdeepsingh.metaapp.ui.chillzone
导入android.os.Bundle
导入android.util.Log
导入com.google.android.material.bottomnavigation.BottomNavigationView
导入androidx.appcompat.app.appcompat活动
导入android.widget.TextView
导入android.widget.Toast
导入com.rishabhdeepsingh.metaapp.R
类ChillZoneActivity:AppCompatActivity(){
私有lateinit变量bottomNavigationView:bottomNavigationView
私有lateinit var textMessage:TextView
private val onNavigationItemSelectedListener=BottomNavigationView.onNavigationItemSelectedListener{item->
println(“~~~~~~~~~~~~~~~~~~~~~~~~~~”)
时间(item.itemId){
R.id.navigation\u主页->{
textMessage.setText(R.string.title\u home)
Toast.makeText(这个“Home”,Toast.LENGTH\u LONG.show())
return@OnNavigationItemSelectedListener符合事实的
}
R.id.navigation\u仪表板->{
textMessage.setText(R.string.title\u仪表板)
Toast.makeText(这个“仪表板”,Toast.LENGTH\u LONG.show())
return@OnNavigationItemSelectedListener符合事实的
}
R.id.navigation\u通知->{
textMessage.setText(R.string.title\u通知)
Toast.makeText(这是“通知”,Toast.LENGTH\u LONG.show())
return@OnNavigationItemSelectedListener符合事实的
}
}
错误的
}
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
bottomNavigationView=findViewById(R.id.bottom\u nav\u视图)
println(“~~~~~~~~~~~~~~~~~~~
Toast.makeText(这是“你好”,Toast.LENGTH\u LONG.show())
textMessage=findviewbyd(R.id.message)
bottomNavigationView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
}
}
ChillZoneFragment.kt

package com.rishabhdeepsingh.metaapp.ui.chillzone
导入android.os.Bundle
导入androidx.fragment.app.fragment
导入android.view.LayoutInflater
导入android.view.view
导入android.view.ViewGroup
导入com.rishabhdeepsingh.metaapp.R
类ChillZoneFragment:Fragment(){
覆盖创建视图(
充气机,
容器:视图组?,
savedInstanceState:捆绑?
):查看{
返回充气机。充气(R.layout.chillzone_碎片,容器,假)
}
}
MainActivity.kt

package com.rishabhdeepsingh.metaapp.ui
导入android.os.Bundle
导入androidx.core.view.GravityCompat
导入androidx.appcompat.app.ActionBarDrawerToggle
导入android.view.MenuItem
导入android.widget.Toast
导入androidx.drawerlayout.widget.drawerlayout
导入com.google.android.material.navigation.NavigationView
导入androidx.appcompat.app.appcompat活动
导入androidx.appcompat.widget.Toolbar
导入com.rishabhdeepsingh.metaapp.R
导入com.rishabhdeepsingh.metaapp.ui.chillzone.ChillZoneFragment
类MainActivity:AppCompatActivity(),NavigationView.OnNavigationItemSelectedListener{
私有lateinit变量抽屉布局:抽屉布局
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val工具栏:toolbar=findviewbyd(R.id.main_工具栏)
设置支持操作栏(工具栏)
val navView:NavigationView=findViewById(R.id.nav_视图)
navView.setNavigationItemSelectedListener(此)
抽屉布局=findViewById(R.id.抽屉布局)
val toggle=ActionBarDrawerToggle(
这个,抽屉布局,工具栏,
R.string.navigation\u抽屉\u打开,
R.string.navigation\u drawer\u close
)
DroperLayout.AddDroperListener(切换)
toggle.syncState()
如果(savedInstanceState==null){
支持碎片管理器
.beginTransaction()
.replace(R.id.fragment\u容器,HomeFragment())
.commit()
navView.setCheckedItem(R.id.home)
}
}
重写函数onBackPressed(){
if(抽屉布局isDrawerOpen(重力比较开始)){
抽屉布局。关闭抽屉(重力公司启动)
}否则{
super.onBackPressed()
}
}
覆盖onNavigationItemSelected(项:MenuItem):布尔值{
//处理导航视图项单击此处。
时间(item.itemId){
R.id.nav_主页->{
//处理相机的动作
支持碎片管理器
.beginTransaction()
.replace(R.id.fragment\u容器,HomeFragment())
.commit()
}
R.id.nav\U区域->{
支持碎片管理器
.beginTransaction()
.更换(R.id.fragment_容器,ChillZoneFragment())
.commit()
}
R.id.nav_幻灯片放映->{
}
R.id.nav_工具->{
}
R.id.nav_份额->{
Toast.makeText(这个“共享”,Toast.LENGTH\u SHORT.show()
}
R.id.nav_发送->{
}
}
抽屉布局。关闭抽屉(重力公司启动)
返回真值
}
}

我希望Toast消息应该与我选择的BottomNavigationView按钮相对应。

似乎底部导航侦听器未被激活
bottomNavigationView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
bottomNavigationView.setOnNavigationItemSelectedListener(this)
class ChillZoneFragment : Fragment() {

    private lateinit var bottomNavigationView: BottomNavigationView
    private lateinit var textMessage: TextView
    private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        println("~~~~~~~~~~~~~~~~~~~~~~~~")
        when (item.itemId) {
            R.id.navigation_home -> {
                textMessage.setText(R.string.title_home)
                Toast.makeText(this, "Home", Toast.LENGTH_LONG).show()
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_dashboard -> {
                textMessage.setText(R.string.title_dashboard)
                Toast.makeText(this, "Dashboard", Toast.LENGTH_LONG).show()
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_notifications -> {
                textMessage.setText(R.string.title_notifications)
                Toast.makeText(this, "Notifications", Toast.LENGTH_LONG).show()
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.chillzone_fragment, container, false)
        bottomNavigationView = view.findViewById(R.id.bottom_nav_view)
        textMessage = view.findViewById(R.id.message)
        bottomNavigationView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
        return view
    }
}