gpt4 book ai didi

java - 如何在android studio中使用MVVM+Databinding设计登录?

转载 作者:行者123 更新时间:2023-12-01 16:24:53 24 4
gpt4 key购买 nike

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

错误:

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;
^

LoginActivity.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 {

}

最佳答案

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

关于java - 如何在android studio中使用MVVM+Databinding设计登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62168863/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com