[英]Android MVP Null object Reference
I am trying to do the MvP Pattern and I am separating firebase queries in the model part. 我正在尝试执行MvP模式,并且正在模型部分中分离Firebase查询。 I get null object reference exception when I call it on the presenter. 在演示者上调用它时,我得到空对象引用异常。
Model: 模型:
public class LoginModelz implements LoginContract.LoginModelz {
private LoginPresenter loginPresenter;
FirebaseAuth mAuth; //= FirebaseAuth.getInstance();
FirebaseUser currentuser;// = mAuth.getCurrentUser();
public LoginModelz(LoginPresenter loginPresenter) {
this.loginPresenter = loginPresenter;
}
public void FirebaseQuery(){
mAuth = FirebaseAuth.getInstance();
currentuser = mAuth.getCurrentUser();
}
}
Presenter: 主持人:
public class LoginPresenter implements LoginContract.LoginPresenter {
private LoginView loginView;
private LoginModelz loginModelz;
public LoginPresenter(LoginView loginView) {
this.loginView = loginView;
}
public LoginPresenter(LoginModelz loginModelz) {
this.loginModelz = loginModelz;
}
@Override
public void OnLogin(String email, String password,final Context context) {
loginModelz.FirebaseQuery();
Log.e("asda",context.toString());
if(email.isEmpty() && password.isEmpty()){
loginView.OnLoginFail("Email and Password is empty");
return;
}
else if(email.isEmpty()){
loginView.OnLoginFail("Email is empty");
return;
}else if(password.isEmpty()){
loginView.OnLoginFail("Password is empty");
return;
}else {
loginView.ProgressShow("ASDASDASDASDSAd");
Log.e("emailpass", email+password);
**loginModelz.mAuth.signInWithEmailAndPassword(email,password)**
.addOnCompleteListener( new OnCompleteListener<AuthResult>()
I was getting an error from the loginmodelz.mAuth
. 我从loginmodelz.mAuth
得到一个错误。 If i put the initializations in the method in the presenter class it works just fine, but when i separated it is getting a null object reference. 如果我将初始化放在presenter类的方法中,它就可以正常工作,但是当我分开时,它将得到一个空对象引用。
Interfaces 接口
public interface LoginContract {
interface LoginView{
void OnLoginSuccess();
void OnLoginFail(String message);
void Onregister();
void ProgressShow(String messsagge);
void ProgressDismiss(String message);
void Alert();
}
interface LoginPresenter{
void OnLogin(String email,String password,final Context context);
void OnVerify(Context context);
}
interface LoginModelz{
void FirebaseQuery();
}
}
This looks suspicious: 这看起来很可疑:
public LoginPresenter(LoginView loginView) {
this.loginView = loginView;
}
public LoginPresenter(LoginModelz loginModelz) {
this.loginModelz = loginModelz;
}
You have to decide on your constructor, if you call the first one you don't have a model and so on. 您必须决定自己的构造函数,如果调用第一个构造函数,则没有模型,依此类推。
You could create your model inside the first constructor and pass it as an argument: 您可以在第一个构造函数中创建模型并将其作为参数传递:
public LoginPresenter(LoginView loginView) {
this.loginView = loginView;
this.loginModel = new LoginModel(this);
}
At least that's what I can suggest from the code you provided. 至少我可以从您提供的代码中提出建议。
You have not initialized loginModelz. 您尚未初始化loginModelz。 Change your presenter constructor implement 更改您的演示者构造器工具
public LoginPresenter(LoginView loginView) {
this.loginView = loginView;
this.loginModelz = new LoginModelz(this);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.