在相应的片段中初始化数据绑定变量
Initializing data binding variable in corresponding fragment
我一直在学习 developer.android.com 上的 Android Kotlin 基础课程,并且 运行 遇到了数据绑定问题。 project I am working on doesn't have solution code provided, but I have been modeling my approach off of a previous similar Codelab.
我正在尝试初始化布局 xml 文件中与每个布局相对应的片段中声明的数据绑定变量,但是当我尝试初始化片段变量时,我收到错误消息:“分类器 'EntreeMenuFragment'没有同伴 object,因此必须在这里初始化”。 build.Gradle dataBinding 和 viewBinding 都设置为 true。
<layout
...
<data>
<variable
name="viewModel"
type="com.example.lunchtray.model.OrderViewModel" />
<variable
name="EntreeMenuFragment"
type="com.example.lunchtray.ui.order.EntreeMenuFragment" />
</data>
...
</layout>
class EntreeMenuFragment : Fragment() {
private var _binding: FragmentEntreeMenuBinding? = null
private val binding get() = _binding!!
private val sharedViewModel: OrderViewModel by activityViewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentEntreeMenuBinding.inflate(inflater, container, false)
val root: View = binding.root
return root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.apply {
lifecycleOwner = viewLifecycleOwner
viewModel = sharedViewModel
EntreeMenuFragment = this@EntreeMenuFragment // ERROR
}
}
....
删除绑定变量的大写即可避免错误:
<variable
name="entreeMenuFragment"
type="com.example.lunchtray.ui.order.EntreeMenuFragment" />
entreeMenuFragment = this@EntreeMenuFragment // fixed
我一直在学习 developer.android.com 上的 Android Kotlin 基础课程,并且 运行 遇到了数据绑定问题。 project I am working on doesn't have solution code provided, but I have been modeling my approach off of a previous similar Codelab.
我正在尝试初始化布局 xml 文件中与每个布局相对应的片段中声明的数据绑定变量,但是当我尝试初始化片段变量时,我收到错误消息:“分类器 'EntreeMenuFragment'没有同伴 object,因此必须在这里初始化”。 build.Gradle dataBinding 和 viewBinding 都设置为 true。
<layout
...
<data>
<variable
name="viewModel"
type="com.example.lunchtray.model.OrderViewModel" />
<variable
name="EntreeMenuFragment"
type="com.example.lunchtray.ui.order.EntreeMenuFragment" />
</data>
...
</layout>
class EntreeMenuFragment : Fragment() {
private var _binding: FragmentEntreeMenuBinding? = null
private val binding get() = _binding!!
private val sharedViewModel: OrderViewModel by activityViewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentEntreeMenuBinding.inflate(inflater, container, false)
val root: View = binding.root
return root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.apply {
lifecycleOwner = viewLifecycleOwner
viewModel = sharedViewModel
EntreeMenuFragment = this@EntreeMenuFragment // ERROR
}
}
....
删除绑定变量的大写即可避免错误:
<variable
name="entreeMenuFragment"
type="com.example.lunchtray.ui.order.EntreeMenuFragment" />
entreeMenuFragment = this@EntreeMenuFragment // fixed