添加FirebaseAuth.getInstance后应用崩溃

时间:2019-02-16 06:46:30

标签: java android firebase firebase-authentication

我昨天添加了Firebase身份验证,并且工作正常,但是今天在编辑一些文件后崩溃了。 仅当我添加“ firebaseAuth = FirebaseAuth.getInstance()”时,应用程序才会崩溃。 如果我将其删除,那么一切正常,但现在我无法添加firebase。

  

SignUp.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);
        }
    }
}

我尝试调试,并且在运行该特定行时崩溃。

  

崩溃日志

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

2 个答案:

答案 0 :(得分:1)

这在使用Fluuter和Firebase的android中最有用

应用级build.gradle

 defaultConfig {      

    minSdkVersion 23

}

在项目级别gradle.properties中添加

android.useAndroidX=true
android.enableJetifier=true

build.gradle中添加

dependencies {
    classpath 'com.android.tools.build:gradle:3.2.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath 'com.google.gms:google-services:4.2.0'
}

如果您不使用Kotlin支持,则可以跳过该Kotlin插件 不要忘记先运行flutter clean然后运行flutter pub upgrade

这些更改帮助我防止了应用程序崩溃,它也应该为您提供帮助。

答案 1 :(得分:0)

您尚未初始化Firebase。将此添加到您的SignUp类的a = [4, 5, 6, 2, 3] smallestNum = min(a) #getting the smallest number in the list indexOfSmallestNum = a.index(smallestNum) # Getting the index of the smallest number # Now, if the indexOfSmallestNum is 0, there is no element before it. #Hence, we return a None. #Else, we return a[indexOfSmallestNum-1], the element before the smallest number. if indexOfSmallestNum == 0: return None else return a[indexOfSmallestNum-1]

onCreate