【问题标题】:is unknown to this NavController inside tab layout选项卡布局中的此 NavController 未知
【发布时间】:2020-02-29 12:08:34
【问题描述】:

我正在实现一个导航组件,但是当我的片段位于选项卡布局内时出现问题,它显示如下错误:

/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 未知

【问题讨论】:

    标签: android android-tablayout android-jetpack-navigation


    【解决方案1】:

    您将 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,而不仅仅是任何片段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-20
      • 2016-07-16
      • 2015-10-28
      • 2021-07-08
      • 2016-06-12
      • 2015-11-28
      • 2017-01-17
      • 1970-01-01
      相关资源
      最近更新 更多