由于我是MVVM和Firebase的新手,所以我尝试创建一个简单的应用程序来注册用户。 SignUp工作正常,但是随之而来的是一个奇怪的错误和应用程序崩溃。由于我也试图了解MVVM,因此,我希望获得一些技巧来完善代码。任何使它更好的帮助。谢谢
MainActivity
package com.example.testingapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
MainViewmodel model;
EditText editEmail;
EditText editPassword;
Button btnSignUp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editEmail = findViewById(R.id.editEmail);
editPassword = findViewById(R.id.editPassword);
btnSignUp = findViewById(R.id.btnSignUp);
btnSignUp.setOnClickListener(this);
model = ViewModelProviders.of(this).get(MainViewmodel.class);
}
@Override
public void onClick(View view) {
model.email = editEmail.getText().toString().trim();
model.password = editPassword.getText().toString().trim();
model.signUp();
}
}
MainViewmodel
package com.example.testingapp;
import androidx.lifecycle.ViewModel;
public class MainViewmodel extends ViewModel {
repo repository;
String email;
String password;
public MainViewmodel(){
if(repository == null){
repository = new repo();
}
}
public void signUp(){
repository.firebaseSignUp(email, password);
}
}
回购
package com.example.testingapp;
import android.util.Log;
import androidx.annotation.NonNull;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import java.util.concurrent.Executor;
import static android.content.ContentValues.TAG;
public class repo {
FirebaseAuth mAuth = FirebaseAuth.getInstance();
public void firebaseSignUp(String email, String password){
Log.d(TAG, "firebaseSignUp: Success" + email + password);
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener((Executor) this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
}
});
}
}
答案 0 :(得分:0)
您可以删除(Executor) this,
。对我来说就是工作。