【发布时间】:2020-08-17 00:01:44
【问题描述】:
我不熟悉将 ViewModel 直接连接到 XML 文件的概念,我真的不知道为什么 Gradle 找不到 ViewModel 类。
下面我发布我的代码和错误日志。
提前感谢您的帮助。
编辑:主要活动只是片段的主机,这个片段实例化 ViewModel
代码
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="tvm"
type="com.uj.bachelor_jlk700.examsystem.screens.test.TestViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".screens.test.TestFragment">
<TextView
android:id="@+id/textView_testFragment_name"
android:layout_width="90dp"
android:layout_height="30dp"
android:text="@{tvm.userName}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.049"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.035" />
错误信息
C:\Users\Jan\AndroidStudioProjects\ExamSystem\app\build\generated\data_binding_base_class_source_out\debug\out\com\uj\bachelor_jlk700\examsystem\databinding\TestFragmentBinding.java:17: error: cannot find symbol
import com.uj.bachelor_jlk700.examsystem.screens.test.TestViewModel;
^
symbol: class TestViewModel
【问题讨论】:
-
你在哪里实例化你的 ViewMdel 在 MainActivity.. 你也没有在 MainActivity 定义绑定对象
-
ViewModel 在 Fragment 中实例化
-
是片段提供的布局吗?
-
是的,是片段布局
标签: android kotlin layout data-binding viewmodel