android - 选项卡布局中的此 NavController 未知
问题描述
我正在实现一个导航组件,但是当我的片段位于选项卡布局内时出现问题,它显示如下错误:
/action_previousMatchFragment_to_detailMatchFragment 对此 NavController 是未知的
class MatchAdapter(private val listMatchItems: List<Match.MatchItem>) :
RecyclerView.Adapter<MatchAdapter.MatchViewHolder>() {
private var itemClickListener:((Match.MatchItem) -> Unit)? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MatchViewHolder {
return MatchViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.item_match, parent, false
)
)
}
override fun getItemCount(): Int = listMatchItems.size
override fun onBindViewHolder(holder: MatchViewHolder, position: Int) {
val matchItem: Match.MatchItem = listMatchItems[position]
holder.bindItem(matchItem)
holder.itemView.setOnClickListener {
itemClickListener?.invoke(matchItem)
}
}
fun setItemClickListener(listener: (matchItem: Match.MatchItem) -> Unit) {
itemClickListener = listener
}
}
class PreviousMatchFragment : Fragment(), PreviousMatchImpl.View {
private var matchItems: MutableList<Match.MatchItem> = mutableListOf()
private val adapter by lazy {
MatchAdapter(
matchItems
)
}
private lateinit var presenter: PreviousMatchPresenter
private var leagueId: String? = null
fun newInstance(leagueId: String): PreviousMatchFragment {
val bundle = Bundle()
bundle.putString("leagueId", leagueId)
val fragment = PreviousMatchFragment()
fragment.arguments = bundle
return fragment
}
private fun readBundle(bundle: Bundle?) {
if (bundle != null) {
leagueId = bundle.getString("leagueId")
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_last_match, container, false)
arguments?.let { readBundle(it) }
adapter.setItemClickListener {
Navigation.findNavController(view).navigate(PreviousMatchFragmentDirections.actionPreviousMatchFragmentToDetailMatchFragment(leagueId))
}
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
presenter = PreviousMatchPresenter(this)
leagueId?.let { presenter.getListMatch(it) }
rv_last_match.layoutManager = LinearLayoutManager(context)
rv_last_match.adapter = adapter
}
override fun setDataList(data: List<Match.MatchItem>) {
matchItems.clear()
matchItems.addAll(data)
adapter.notifyDataSetChanged()
}
}
完整的错误是:
java.lang.IllegalArgumentException:导航目的地 com.example.rifqi.footballapp:id/action_previousMatchFragment_to_detailMatchFragment 对此 NavController 是未知的
解决方案
您正在将R.layout.fragment_last_match
NavController 设置为片段。这不是您使用 NavController 的方式。
您的 Navigation.findNavController(view).navigate(PreviousMatchFragmentDirections.actionPreviousMatchFragmentToDetailMatchFragment(leagueId))
应该
Navigation.findNavController(R.ID.OF_NAV_HOST_FRAGMENT).navigate(PreviousMatchFragmentDirections.actionPreviousMatchFragmentToDetailMatchFragment(leagueId))
其中 R.ID.OF_NAV_HOST_FRAGMENT 是 NavHostFragment 的 id,而不仅仅是任何片段。
推荐阅读
- react-native - 尝试安装 @testing-library/react-native 时出错
- azure - 动态 ARM 模板参数值
- python - pywin32、win32gui窗口捕获黑屏
- bash - bash 变量值检查和修改
- python - Flask 执行和同步生成另一个子进程的 python 子进程
- html - 更改引导导航栏上活动锚链接的边框颜色
- amazon-web-services - CloudTrail 可以在事件历史记录中记录 SQS ReveiceMessage 和 SendMessage 事件吗?
- python-3.x - Django InlineFormset:基于模型数量的大小
- javascript - React,Material Ui:无法在隐藏菜单上映射值
- c# - 数据库模式版本控制