[英]App crashes after adding FirebaseAuth.getInstance
I had added firebase authentication yesterday and it was working fine but today after editing some files it is crashing.我昨天添加了 firebase 身份验证并且它工作正常但是今天在编辑一些文件后它崩溃了。 App only crashes when I add "firebaseAuth =FirebaseAuth.getInstance()".
仅当我添加“firebaseAuth =FirebaseAuth.getInstance()”时,应用程序才会崩溃。 If I remove it then all things work fine but now I cannot add firebase.
如果我删除它,那么一切正常,但现在我无法添加 firebase。
SignUp.java
注册.java
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
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;
public class SignUp extends AppCompatActivity implements View.OnClickListener {
private TextView alreadyRegistered;
private EditText mEmailView;
private EditText mPasswordView;
private Button signup;
private FirebaseAuth firebaseAuth;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
progressDialog=new ProgressDialog(this);
mEmailView = (EditText) findViewById(R.id.email_signup);
mPasswordView = (EditText) findViewById(R.id.password_signup);
signup=(Button)findViewById(R.id.signup);
alreadyRegistered=(TextView)findViewById(R.id.already_registered);
// firebaseAuth =FirebaseAuth.getInstance();
//// if(firebaseAuth.getCurrentUser() != null){
//// //that means user is already logged in
//// //so close this activity
//// finish();
////
//// //and open profile activity
//// startActivity(new Intent(getApplicationContext(), Home.class));
//// }
alreadyRegistered.setOnClickListener(this);
signup.setOnClickListener(this);
}
private void registerUser() {
//getting email and password from edit texts
String email = mEmailView.getText().toString().trim();
String password = mPasswordView.getText().toString().trim();
//checking if email and passwords are empty
if (TextUtils.isEmpty(email)) {
Toast.makeText(this, "Please enter email", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(this, "Please enter password", Toast.LENGTH_LONG).show();
return;
}
//if the email and password are not empty
//displaying a progress dialog
progressDialog.setMessage("Registering Please Wait...");
progressDialog.show();
//creating a new user
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
//checking if success
if (task.isSuccessful()) {
//display some message here
Toast.makeText(SignUp.this, "Successfully registered", Toast.LENGTH_LONG).show();
Intent intent = new Intent(SignUp.this, MainActivity.class);
startActivity(intent);
} else {
//display some message here
Toast.makeText(SignUp.this, "Registration Error", Toast.LENGTH_LONG).show();
}
// progressDialog.dismiss();
}
});
}
@Override
public void onClick(View v) {
if(v==signup)
{
registerUser();
}
if(v==alreadyRegistered)
{
Intent intent = new Intent(SignUp.this,MainActivity.class);
startActivity(intent);
}
}
}
I tried debugging and it crashes when I run that particular line.我试过调试,当我运行那条特定的行时它崩溃了。
Crash Logs
崩溃日志
2019-02-16 14:43:23.569 25053-25053/com.example.android.nirog E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.nirog, PID: 25053
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.nirog/com.example.android.nirog.SignUp}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.android.nirog. Make sure to call FirebaseApp.initializeApp(Context) first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2949)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1745)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:200)
at android.app.ActivityThread.main(ActivityThread.java:6956)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:519)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:836)
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.android.nirog. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.2:240)
at com.google.firebase.auth.FirebaseAuth.getInstance(Unknown Source:1)
at com.example.android.nirog.SignUp.onCreate(SignUp.java:44)
at android.app.Activity.performCreate(Activity.java:7225)
at android.app.Activity.performCreate(Activity.java:7216)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2902)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1745)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:200)
at android.app.ActivityThread.main(ActivityThread.java:6956)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:519)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:836)
2019-02-16 14:43:23.623 25053-25053/com.example.android.nirog I/Process: Sending signal. PID: 25053 SIG: 9
You have not initialised firebase.您尚未初始化 Firebase。 Add this in
onCreate
of your SignUp class将此添加到您的 SignUp 类的
onCreate
中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
FirebaseApp.initializeApp(Context)
This is most helpful in android with fluuter and firebase这在带有fluuter和firebase的android中最有帮助
in app level build.gradle
在应用级
build.gradle
defaultConfig {
minSdkVersion 23
}
in project level gradle.properties
add在项目级别
gradle.properties
添加
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
android.enableR8=true
in build.gradle
add在
build.gradle
添加
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.2.0'
}
you can skip that Kotlin plugin if you are not using Kotlin support Dont forget to run flutter clean
and then flutter pub upgrade
如果您不使用 Kotlin 支持,则可以跳过该 Kotlin 插件 不要忘记运行
flutter clean
然后flutter pub upgrade
These changes helped me preventing my app crashes, It should help you too.这些更改帮助我防止了我的应用程序崩溃,它也应该对您有所帮助。
Make sure to call FirebaseApp.initializeApp(Context) first in Android method added in MyApplication class in your project like:确保在项目中的 MyApplication class 中添加的 Android 方法中首先调用 FirebaseApp.initializeApp(Context) ,例如:
override fun onCreate() {
super.onCreate()
{
FirebaseApp.initializeApp(Context);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.