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”。我无法解决以上粗体行中的错误。在这种情况下,我该怎么办?
最佳答案
按照Navigate to a destination documentation,导入的androidx.navigation.fragment.findNavController
不带任何参数(它会找到当前Fragment的父NavHostFragment,并且不需要NavHostFragment的ID)。
您编写的行(即findNavController(R.id.nav_host_fragment)
和setupActionBarWithNavController()
)是您要在Activity中(而不是在Fragment中)调用的方法。