【问题标题】:Cant find layout ID when going from Fragment to Fragment从 Fragment 到 Fragment 时找不到布局 ID
【发布时间】:2021-12-28 22:29:39
【问题描述】:

我有一个底部导航应用程序,我们需要能够从一个片段转到另一个片段(使用后退按钮返回第一个片段)。 我不知道要为容器视图 ID 传递什么以传递给替换调用。我尝试过的所有内容都显示未解决的参考。

class FoodLogFragment : Fragment() {
   private var _binding: FragmentFoodlogBinding? = null
   private lateinit var adapter: MealAdapter
   var startDate: Date = Date()

   // This property is only valid between onCreateView and
   // onDestroyView.
   private val binding get() = _binding!!

   override fun onCreateView(
       inflater: LayoutInflater,
       container: ViewGroup?,
       savedInstanceState: Bundle?
   ): View? {
       _binding = FragmentFoodlogBinding.inflate(inflater, container, false)
       val root: View = binding.root

       return root
   }

   fun handleAddButton(mealType: MealType) {
       val transaction = requireActivity().supportFragmentManager.beginTransaction()
                        //problem is here
                        //there are no layout ids after R.id, or R.layout
       transaction.replace(R.id.fragment_foodcategory, FoodCategoryFragment())
       transaction.disallowAddToBackStack()
       transaction.commit()
   }

【问题讨论】:

  • 您使用的是requireActivity().supportFragmentManager,因此 ID 必须是您活动布局中的容器。请包含您的活动的布局 XML。

标签: android kotlin android-fragments


【解决方案1】:

原来这已经以某种方式进入了我的标题

import android.R

需要

import edu.trafficlightlog.R

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多