java - 如何在android studio中设计登录使用MVVM+Databinding?
问题描述
我是新手,没有编程经验。现在使用CODELABS.developer.google.com和developer.android.com学习java+android,但是在设计“登录”模块的时候遇到了问题。这个问题花了我3天时间。我每天都在谷歌上搜索解决方案,但我仍然没有解决。
错误:
C:\Users\Admin\AndroidStudioProjects\exmaple\app\build\generated\data_binding_base_class_source_out\debug\out\com\exmaple\databinding\FragmentEmailEditBinding.java:15: ����: �Ҳ�������
import com.exmaple.ViewModel;
^
����: �� ViewModel
�: ����� com.exmaple
C:\Users\Admin\AndroidStudioProjects\exmaple\app\build\generated\data_binding_base_class_source_out\debug\out\com\exmaple\databinding\FragmentEmailEditBinding.java:33: ����: �����ViewModel.Login������
protected ViewModel.Login.EmailAuthViewModel mEmailAuthViewModel;
^
登录活动.java
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
FragmentEmailEditBinding binding = DataBindingUtil.setContentView(this,R.layout.fragment_email_edit);
EmailAuthViewModel emailAuthViewModel = new ViewModelProvider(this).get(EmailAuthViewModel.class);
binding.getEmailAuthViewModel();
binding.setLifecycleOwner(this);
FragmentManager fragmentManager = getSupportFragmentManager();
EmailAuthFragment emailAuthFragment = new EmailAuthFragment();
fragmentManager.beginTransaction()
.add(R.id.frag_signIn,emailAuthFragment).commit();
}
}
fragment_email_edit.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="emailAuthViewModel"
type="com.example.ViewModel.Login.EmailAuthViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp">
<EditText
android:id="@+id/fieldEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:hint="@string/hint_email"
android:inputType="textEmailAddress"
android:text="@={emailAuthViewModel.email}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@id/fieldPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.25"
app:layout_constraintVertical_chainStyle="packed" />
<EditText
android:id="@+id/fieldPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:hint="@string/hint_password"
android:inputType="textPassword"
android:text="@{emailAuthViewModel.password}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/fieldEmail"
app:layout_constraintVertical_bias="0.25"
app:layout_constraintVertical_chainStyle="packed" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
EmailAuthViewModel.java
public class EmailAuthViewModel extends ViewModel {
public MutableLiveData<String> email = new MutableLiveData<>();
public MutableLiveData<String> password = new MutableLiveData<>();
public EmailAuthViewModel(MutableLiveData<String> email, MutableLiveData<String> password) {
this.email = email;
this.password = password;
}
}
EmailAuthModel.java
class EmailAuthModel {
}
解决方案
您是否尝试将包“ViewModel”重命名为“viewmodel”?因为“ViewModel”是默认类之一的名称。
而且我还在你的包中发现了一个奇怪的地方,在错误“com.exmaple”中,但在你的布局文件中使用“com.example”。
推荐阅读
- linux - Address already in use error binding UDP socket after socket closed
- android - 如何防止 BiometricPrompt 在三星设备上打开黑屏窗口?
- karate - 空手道 - 如何从 Json 响应中提取并用作大纲场景中的数据驱动数据
- python - 将文本添加到彩色输入文本框 pygame
- javascript - 了解记忆的斐波那契函数
- python - Web Scraping - Python - 需要帮助
- java - 在 Java 中流式传输到 Icecast2 时交叉淡化歌曲
- ruby-on-rails - 使用 ActiveRecord 回调
- html - 箭头悬停在后面添加一个圆形 div
- amazon-web-services - AWS Lambda:获得销售