【发布时间】:2019-06-24 20:38:56
【问题描述】:
尝试从我的视图模型中运行布局文件中的方法,但是我不断从我的布局文件中获取error: cannot find symbol class ViewModels。布局文件是一个片段。我尝试使缓存无效并重新同步,但没有帮助。我需要向片段本身添加任何内容吗?我看到人们使用数据绑定来启动片段,但我已经阅读了它的可选内容。
更新:取出 OnClick 方法进行测试,它仍然抛出错误。我想问题出在我的减速上,但是,我想知道为什么。当我编辑布局时,当我输入视图模型名称时,路径就会显示出来。
Update2:尝试将变量中的类型设置为等于启动片段进行测试的活动路径,并且构建得很好。必须有一种方法可以添加不是我的活动的导入。
布局文件
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="viewTest"
type="rangers.socmanv2.ViewModels.BattleRhythmViewModel" />
</data>
<android.support.constraint.ConstraintLayout 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"
android:id="@+id/battleRhythmMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.BattleRhythmFrag">
<Button
android:id="@+id/newBattle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="76dp"
android:layout_marginLeft="76dp"
android:layout_marginTop="88dp"
android:text="New Battle Rhythm"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<Button
android:id="@+id/editBattle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="76dp"
android:layout_marginLeft="76dp"
android:layout_marginTop="50dp"
android:text="Edit Battle Rhythm"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/newBattle" />
<Button
android:id="@+id/deleteBattle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="76dp"
android:layout_marginLeft="76dp"
android:layout_marginTop="50dp"
android:text="Delete Battle Rhythm"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editBattle" />
</android.support.constraint.ConstraintLayout>
</layout>
片段
public class BattleRhythmFrag extends Fragment {
private BattleRhythmViewModel mViewModel;
private View view;
private Button test;
public static BattleRhythmFrag newInstance() {
return new BattleRhythmFrag();
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.battle_rhythm_fragment, container, false);
mViewModel = new BattleRhythmViewModel();
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewModel = ViewModelProviders.of(this).get(BattleRhythmViewModel.class);
// TODO: Use the ViewModel
}
}
查看模型
public class BattleRhythmViewModel extends ViewModel {
public void launchNewFragment(FragmentManager fragM)
{
Log.d("viewmodel","hitting launchNewFrag");
HomeFrag test = new HomeFrag();
fragM.beginTransaction().replace(R.id.homeContent,test,"launched"+test);
}
public void test()
{Log.d("viewmodel","hitting test");
}
}
【问题讨论】:
-
你是否在 build.gradle 中添加了类似 *dataBinding { enabled = true } 的 gradle 配置?
-
您的 onclick 方法 lambda 有错误更改
android:onClick="@{() -> viewTest.test()}"和android:onClick="@{(v) -> viewTest.test()}"在 lambda 中添加您的视图 -
是的 @JonathasNascimento build.gradle 已启用。
-
@mahdishahbazi 我去掉了那条线,现在只是我的可变减速给了我错误
标签: android android-databinding