本文介绍了为了许多有趣的论点Fragment.findNavController()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.drawerlayout.widget.DrawerLayout
import com.example.tender.R
import com.example.tender.databinding.FragmentAvailableTenderBinding
import com.example.tender.databinding.FragmentLoginBinding
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.NavigationUI

/**
 * A simple [Fragment] subclass.
 */
class AvailableTenderFragment : Fragment() {

    private lateinit var drawerLayout: DrawerLayout

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val binding = FragmentAvailableTenderBinding.inflate(inflater)

        drawerLayout = binding.drawerLayout

        **val navController = this.findNavController(R.id.nav_host_fragment)**

        **NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)**

        NavigationUI.setupWithNavController(binding.navView, navController)

        binding.root

    }
}

当我将鼠标悬停在findnavcontroller上时,将显示对于许多有趣的参数,在androidx.navigation.fragment中定义的Fragment.findNavController():NAvcontroller".我无法解决以上粗体行中的错误.在这种情况下,我该怎么办?

"To many arguments for public fun Fragment.findNavController():NAvcontroller defined in androidx.navigation.fragment" is showing when i hover on findnavcontroller.I am unable to resolve the error on the above bold lines.In this case what should I do?

推荐答案

按照导航到目标文档,您导入的 androidx.navigation.fragment.findNavController 不带任何参数(它会找到当前Fragment的父级NavHostFragment,不需要ID为NavHostFragment).

As per the Navigate to a destination documentation, the androidx.navigation.fragment.findNavController you've imported takes no parameters (it finds the parent NavHostFragment of the current Fragment and doesn't need the ID of the NavHostFragment).

您编写的行,即 findNavController(R.id.nav_host_fragment) setupActionBarWithNavController()是您在Activity中调用的方法,而不是在片段.

The lines you've written, namely findNavController(R.id.nav_host_fragment) and setupActionBarWithNavController() are methods you'd call in an Activity, not in a Fragment.

这篇关于为了许多有趣的论点Fragment.findNavController()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 02:39