Попытка запустить метод в файле макета из моей модели представления, но я продолжаю получать 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");
}
}