“嗨,我在选择选项卡时使用TabLayout和framlayout填充了片段,并且我还使用导航分别导航了每个片段的第二个屏幕和第三个屏幕。我的问题是当我从fragmentA或FragmentB ..ect。导航控制器仅获取BaseFragment ID,如何更改所选片段的ID,以及如何从第二和第三屏幕(如果需要)中的所选片段进行导航。
错误是:IllegalArgumentException:此NavController未知导航目标xxx“
class BaseFragment : Fragment() {
private var tabLayout: TabLayout? = null
private var frameLayout: FrameLayout? = null
private var fragment: Fragment? = null
private var fragmentTransaction: FragmentTransaction? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val viewLayout = inflater.inflate(R.layout.fragment_base, container, false)
tabLayout = viewLayout.findViewById(R.id.tabLayout)
frameLayout = viewLayout.findViewById(R.id.frameLayout)
tabLayout!!.getTabAt(0)!!.icon!!.setColorFilter(
Color.parseColor("#cf1804"),
PorterDuff.Mode.SRC_IN
)
fragment = FragmentA()
fragmentTransaction = fragmentManager!!.beginTransaction()
fragmentTransaction!!.replace(R.id.frameLayoutBase, fragment!!)
fragmentTransaction!!.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
fragmentTransaction!!.commit()
tabLayout!!.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabReselected(tab: TabLayout.Tab?) {
}
override fun onTabUnselected(tab: TabLayout.Tab?) {
}
override fun onTabSelected(tab: TabLayout.Tab?) {
when (tab!!.position) {
0 -> fragment = FragmentA()
1 -> fragment = FragmentB()
2 -> fragment = FragmentC()
3 -> fragment = FragmentD()
4 -> fragment = FragmentE()
}
val fm = fragmentManager!!.beginTransaction()
fm.replace(R.id.frameLayoutBase, fragment!!)
fm.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
fm.commit()
}
})
return viewLayout
}
}
FragmentA:
class FragmentA : Fragment() {
private var introVideo: VideoView? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_introduction, container, false)
introVideo = view.findViewById<VideoView>(R.id.introVideoPage)
val mediaControll = MediaController(this.context)
mediaControll.setAnchorView(introVideo)
introVideo!!.setMediaController(mediaControll)
val db = FirebaseFirestore.getInstance()
val collection = db.collection("videos_list").document("introduction")
collection.get().addOnSuccessListener { documentSnapshot ->
if (documentSnapshot.exists()) {
val introData = documentSnapshot.toObject(IntroVideoDataModel::class.java)
introVideo!!.setVideoPath(introData!!.video_path)
introVideo!!.start()
} else {
return@addOnSuccessListener
}
}.addOnFailureListener({
val alertDialog = AlertDialog.Builder(this.context).create()
alertDialog.setTitle("Alert")
alertDialog.setMessage("This video is not able to play...")
alertDialog.setButton(
AlertDialog.BUTTON_NEUTRAL,
"OK",
{ dialog: DialogInterface?, which: Int ->
return@setButton
})
alertDialog.show()
// Log.d("Video failed Msg", "${it.localizedMessage}")
})
return view
}
}
“并且FragmentB ... E也具有类似的不同类型,每个具有多个屏幕的片段都需要移动,但是问题是我不能这样做。请帮助解决这个问题,我也是android开发的新手预先感谢。”