Firebase Auth使android stuio中的应用程序崩溃

时间:2019-03-29 14:23:27

标签: android firebase firebase-authentication

  

当我连接到efirebase身份验证以进行注册活动时,我正在制作一个聊天应用程序。

     

03-29 19:26:37.449 17602-17602 / com.developerillusion.hive E / AndroidRuntime:致命异常:main       流程:com.developerillusion.hive,PID:17602       java.lang.RuntimeException:无法启动活动ComponentInfo {com.developerillusion.hive / com.developerillusion.hive.RegisterActivity}:java.lang.IllegalStateException:在此过程com.developerillusion.hive中未初始化默认FirebaseApp。确保首先调用FirebaseApp.initializeApp(Context)。           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)           在android.app.ActivityThread.access $ 800(ActivityThread.java:155)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1317)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:135)           在android.app.ActivityThread.main(ActivityThread.java:5343)           在java.lang.reflect.Method.invoke(本机方法)           在java.lang.reflect.Method.invoke(Method.java:372)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)        由以下原因引起:java.lang.IllegalStateException:在此进程com.developerillusion.hive中未初始化默认的FirebaseApp。确保首先调用FirebaseApp.initializeApp(Context)。           在com.google.firebase.FirebaseApp.getInstance(SourceFile:218)           位于com.google.firebase.auth.FirebaseAuth.getInstance(未知来源)           在com.developerillusion.hive.RegisterActivity.onCreate(RegisterActivity.java:35)           在android.app.Activity.performCreate(Activity.java:6010)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)           在android.app.ActivityThread.access $ 800(ActivityThread.java:155)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1317)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:135)           在android.app.ActivityThread.main(ActivityThread.java:5343)           在java.lang.reflect.Method.invoke(本机方法)           在java.lang.reflect.Method.invoke(Method.java:372)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

        package com.developerillusion.hive;

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 RegisterActivity extends AppCompatActivity
{
    private Button CreateAccountButton;
    private EditText UserEmail, UserPassword;
    private TextView AlreadyHaveAccountLink;

    private FirebaseAuth mAuth;

    private ProgressDialog loadingBar;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        mAuth = FirebaseAuth.getInstance();


        InitializeFields();


        AlreadyHaveAccountLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                SendUserToLoginActivity();
            }
        });




        CreateAccountButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                CreateNewAccount();

            }
        });

    }

    private void CreateNewAccount()
    {
        String email = UserEmail.getText().toString();
        String password = UserPassword.getText().toString();


        if (TextUtils.isEmpty(email))
        {
            Toast.makeText(this, "Please enter your Email", Toast.LENGTH_SHORT).show();
        }

        if (TextUtils.isEmpty(password))
        {
            Toast.makeText(this, "Please enter your Password", Toast.LENGTH_SHORT).show();
        }
        else
        {
            loadingBar.setTitle("Creating New Account");
            loadingBar.setMessage("Please wait, while we are creating new account for you");
            loadingBar.setCanceledOnTouchOutside(true);
            loadingBar.show();

            mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task)
                {
                    if (task.isSuccessful())
                    {
                        SendUserToLoginActivity();
                        Toast.makeText(RegisterActivity.this, "Account Created Successfully", Toast.LENGTH_SHORT).show();
                        loadingBar.dismiss();
                    }
                    else
                    {
                        String message = task.getException().toString();
                        Toast.makeText(RegisterActivity.this, "Error: " + message, Toast.LENGTH_SHORT).show();
                        loadingBar.dismiss();
                    }

                }
            });
        }

    }

    private void InitializeFields()
    {
        CreateAccountButton = (Button) findViewById(R.id.register_button);
        UserEmail = (EditText) findViewById(R.id.register_email);
        UserPassword = (EditText) findViewById(R.id.register_password);
        AlreadyHaveAccountLink = (TextView) findViewById(R.id.already_have_account_link);

        loadingBar = new ProgressDialog(this);

    }

    private void SendUserToLoginActivity()
    {
        Intent loginIntent = new Intent(RegisterActivity.this, LoginActivity.class);
        startActivity(loginIntent);
    }
}

1 个答案:

答案 0 :(得分:1)

您缺少Firebase的初始化,即在您的onCreate方法中使用下面的代码

@Override
public void onCreate() {
super.onCreate();
FirebaseApp.initializeApp(this);

//rest of the code
}

并确保在应用gradle的末尾应用插件:“ com.google.gms.google-services”