好的,当我点击注册按钮注册新用户时,它会给我以下行为。
错误:
尝试调用虚拟方法'com.google.android.gms.tasks.Task com.google.android.gms.common.api.GoogleApi.zzb(com.google.android.gms.common.api.internal.zzdf)” 在空对象引用上
错误来源:
在 com.example.cmd.pop.Fragments.RegisterFragment $ 1.onClick(RegisterFragment.java:64)
它指向这一行:
mAuth.createUserWithEmailAndPassword(mail,pass)
.addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>()
RegisterFragment类:
public class RegisterFragment extends Fragment {
private EditText mMail,mPass,mPassConf;
private Button mLogin,mRegister;
private FirebaseAuth mAuth;
private String mail,pass,passConf;
public RegisterFragment() {
mAuth = FirebaseAuth.getInstance();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.register_fragment_layout,container,false);
find(v);
onClick();
return v;
}
private void find(View v) {
mMail = (EditText)v.findViewById(R.id.registerName);
mPass = (EditText)v.findViewById(R.id.registerPassword);
mPassConf = (EditText)v.findViewById(R.id.registerPasswordConfirmation);
mLogin = (Button)v.findViewById(R.id.btnLoginRegister);
mRegister = (Button)v.findViewById(R.id.btnRegisterRegister);
}
private void onClick() {
mRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mail = mMail.getText().toString();
pass = mPass.getText().toString();
passConf = mPassConf.getText().toString();
if(!isEmpty(mail) && !isEmpty(pass) && !isEmpty(passConf) ) {
if(TextUtils.equals(pass,passConf)) {
mAuth.createUserWithEmailAndPassword(mail,pass)
.addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
getActivity().startActivity(new Intent(getActivity(), MainActivity.class));
getActivity().finish();
} else {
Toast.makeText(getActivity(), "Error registering....", Toast.LENGTH_SHORT).show();
}
}
});
} else {
Toast.makeText(getActivity(), "Password didn't match.", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getActivity(), "All fields need to be filled.", Toast.LENGTH_SHORT).show();
}
}
});
mLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment loginFragment = new LoginFragment();
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.loginActivityContainer,loginFragment).commit();
}
});
}
private boolean isEmpty(String name) {
return TextUtils.isEmpty(name);
}
应用级Gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "27.0.0"
defaultConfig {
applicationId "com.example.cmd.pop"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:support-v4:26.1.0'
implementation 'com.google.firebase:firebase-database:11.6.0'
implementation 'com.google.firebase:firebase-auth:11.6.0'
compile 'com.android.support:design:26.1.0'
compile 'com.android.support:cardview-v7:26.1.0'
compile 'com.android.support:recyclerview-v7:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
exclude group: 'com.android.support', module: 'support-annotations'
})
}
apply plugin: 'com.google.gms.google-services'
项目级别Gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0-alpha03'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'com.google.gms:google-services:3.1.0'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
答案 0 :(得分:0)
在验证控制台中启用的电子邮件/密码后,尝试将firebase降级到较早版本
implementation 'com.google.firebase:firebase-auth: 9.6.0'
答案 1 :(得分:0)
我解决了错误,问题出在模拟器中我需要更新模拟器上的google play服务,并且在更新后一切正常。