gpt4 book ai didi

android - 为了许多有趣的论点Fragment.findNavController()

转载 作者:行者123 更新时间:2023-12-02 13:44:54 26 4
gpt4 key购买 nike

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中)调用的方法。

关于android - 为了许多有趣的论点Fragment.findNavController(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59728421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com