我正在做我的第一个项目
我使用了FirebaseAuth和Firebase实时数据库
一开始我仔细地遵循了每个步骤,一切都很好,我的项目已成功连接到数据库(两个星期前),我什至已经测试了我的数据库,但是更新我的android studio版本后,出了点问题:(
每次我单击按钮时,我的应用都会崩溃。
我真的不知道出什么问题了吗?在一切还好之前
“我阅读了有关api键的文章,并检查了是否有错”
这是我的注册码:
package com.example.lastone;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.santalu.maskedittext.MaskEditText;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.widget.Toast;
import com.basgeekball.awesomevalidation.AwesomeValidation;
import com.basgeekball.awesomevalidation.ValidationStyle;
import com.basgeekball.awesomevalidation.utility.RegexTemplate;
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 com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class Register2 extends AppCompatActivity {
MaskEditText license,phone;
private EditText email,pass1,pass2;
private Button but;
AwesomeValidation awesomeValidation;
private FirebaseDatabase rootNode;
//realtime
DatabaseReference reference;
//authinicate
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register2);
//validation
awesomeValidation=new AwesomeValidation(ValidationStyle.BASIC);
updateUI();
//authinication
firebaseAuth= FirebaseAuth.getInstance();
}
private void updateUI() {
license=findViewById(R.id.license);
email=(EditText)findViewById(R.id.email);
phone=findViewById(R.id.phone);
pass1=(EditText)findViewById(R.id.pass1);
pass2=(EditText)findViewById(R.id.pass2);
but=(Button)findViewById(R.id.but);
String regexPassword = "(?=.*[a-z])(?=.*[A-Z])(?=.*[\\d])(?=.*[~`!@#\\$%\\^&\\*\\(\\)\\-_\\+=\\{\\}\\[\\]\\|\\;:\"<>,./\\?]).{8,}";
String Regex = ("[0-9]{10}");
awesomeValidation.addValidation(Register2.this,R.id.license, Regex, R.string.licensee);
awesomeValidation.addValidation(Register2.this,R.id.email,android.util.Patterns.EMAIL_ADDRESS, R.string.emailrr);
awesomeValidation.addValidation(Register2.this,R.id.phone, RegexTemplate.TELEPHONE, R.string.phonen);
awesomeValidation.addValidation(Register2.this,R.id.pass1,regexPassword,R.string.pass11);
awesomeValidation.addValidation(Register2.this,R.id.pass2,R.id.pass1,R.string.pass22);
but.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
signUp();
// send to the database
rootNode= FirebaseDatabase.getInstance();
reference=rootNode.getReference("users");
//get all the values from the textfield
String License =license.getText().toString();
String Phone_number =phone.getText().toString();
String Email =email.getText().toString();
String password =pass1.getText().toString();
UserHelperClass helper=new UserHelperClass(License , Phone_number, Email, password);
reference.child(License).setValue(helper);
//validate
if(awesomeValidation.validate())
{
Toast.makeText(Register2.this, "Data Received Successfully", Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(Register2.this, "Error", Toast.LENGTH_SHORT).show();
}
});
}
private void signUp() {
firebaseAuth.createUserWithEmailAndPassword(email.getText().toString(), pass1.getText().toString())
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
FirebaseUser user = firebaseAuth.getCurrentUser();
String email=user.getEmail();
String name=user.getDisplayName();
Toast.makeText(Register2.this, "Name : "+name+"\n"+"Email : "+email,
Toast.LENGTH_SHORT).show();
Intent intent=new Intent(Register2.this,Login.class);
startActivity(intent);
} else {
// If sign in fails, display a message to the user.
Toast.makeText(Register2.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
// updateUI(null);
}
// ...
}
});
}
}
登录代码:
package com.example.lastone;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
public class Login extends AppCompatActivity
{
private EditText email,pass;
private Button lo;
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
email=(EditText)findViewById(R.id.email);
pass=(EditText)findViewById(R.id.pass);
lo=(Button)findViewById(R.id.lo);
firebaseAuth= FirebaseAuth.getInstance();
lo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Signin();
}
});
}
private void Signin()
{
firebaseAuth.signInWithEmailAndPassword(email.getText().toString(), pass.getText().toString())
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
FirebaseUser user = firebaseAuth.getCurrentUser();
String email=user.getEmail();
String name=user.getDisplayName();
Toast.makeText(Login.this,"Logged in successuly",
Toast.LENGTH_SHORT).show();
} else {
// If sign in fails, display a message to the user.
Toast.makeText(Login.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
// Intent intent=new Intent(Login.this,profile.class);
}
}
});
}
}
错误:
2020-04-04 02:25:41.923 7642-7642/com.example.lastone E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lastone, PID: 7642
java.lang.IllegalArgumentException: Given String is empty or null
at com.google.android.gms.common.internal.Preconditions.checkNotEmpty(Unknown Source)
at com.google.firebase.auth.FirebaseAuth.signInWithEmailAndPassword(com.google.firebase:firebase-auth@@19.3.0:205)
at com.example.lastone.Login.Signin(Login.java:53)
at com.example.lastone.Login.access$000(Login.java:27)
at com.example.lastone.Login$1.onClick(Login.java:46)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
还建立了gradle:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.lastone"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.basgeekball:awesome-validation:4.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-database:19.2.1'
implementation 'com.google.firebase:firebase-auth:19.3.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.github.santalu:mask-edittext:1.0.2'
}
截止日期是本周,请帮助:((
最佳答案
添加这些依赖项,然后再次运行您的项目
implementation 'com.google.firebase:firebase-analytics:17.2.3'
implementation 'com.google.firebase:firebase-core:17.2.3'
关于firebase - android studio FirebaseAuth(IllegalArgumentException:给定的String为空或null),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61022227/