如何在安卓工作室中设计使用 MVVM+数据绑定登录?



我是新手,没有编程经验。现在我用 CODELABS.developer.google.com 和 developer.android.com 来学习java+android,但是在设计"登录"模块时遇到了问题。这个问题花了我 3 天的时间。我每天都在谷歌上搜索解决方案,但我仍然没有解决它。

错误:

C:UsersAdminAndroidStudioProjectsexmapleappbuildgenerateddata_binding_base_class_source_outdebugoutcomexmapledatabindingFragmentEmailEditBinding.java:15: ����: �Ҳ�������
import com.exmaple.ViewModel;
^
����:   �� ViewModel
�: ����� com.exmaple
C:UsersAdminAndroidStudioProjectsexmapleappbuildgenerateddata_binding_base_class_source_outdebugoutcomexmapledatabindingFragmentEmailEditBinding.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>

电子邮件身份验证视图模型.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;
}
}

电子邮件身份验证模型.java

class EmailAuthModel {
}

我的猜测是,这一行binding.getEmailAuthViewModel(); 抛出NPE,因为您没有将视图模型传递给任何地方的绑定。我认为您想将视图模型传递给数据绑定文件,而不是从中获取。

您是否尝试过将包"ViewModel"重命名为"viewmodel"?因为"视图模型"是默认类之一的名称。

我还在您的软件包中发现了一个奇怪之处,在错误"com.exmaple"中,但在您的布局文件中使用"com.example"。

相关内容

  • 没有找到相关文章

最新更新