java - 默认FirebaseApp在此过程中未初始化official96.yohan.lms。确保首先调用 FirebaseApp.initializeApp(Context)

标签 java android firebase

我想从我的 Android 应用程序将数据上传到 fire-base。当我尝试验证我的注册表单时,出现此错误。我已经尝试过这些解决方案,但对我不起作用。

我在下面分享了错误和注册 java 类。

这是我的错误

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: official96.yohan.lms, PID: 4434
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)
        at android.view.View.performClick(View.java:5610)
        at android.view.View$PerformClick.run(View.java:22265)
        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:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
        at android.view.View.performClick(View.java:5610) 
        at android.view.View$PerformClick.run(View.java:22265) 
        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:6077) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
     Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process official96.yohan.lms. Make sure to call FirebaseApp.initializeApp(Context) first.
        at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.1:219)
        at com.google.firebase.auth.FirebaseAuth.getInstance(Unknown Source)
        at official96.yohan.lms.signup.signupButtonClicked(signup.java:58)
        at java.lang.reflect.Method.invoke(Native Method) 
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384) 
        at android.view.View.performClick(View.java:5610) 
        at android.view.View$PerformClick.run(View.java:22265) 
        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:6077) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

还有我的java类:

package official96.yohan.lms;

import android.content.Intent;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
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.FirebaseApp;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class signup extends AppCompatActivity
{
    EditText mail,mophone,pswd,usrusr;
    TextView lin,sup;

    private FirebaseAuth mAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);

        FirebaseApp.initializeApp(this);
        mAuth = FirebaseAuth.getInstance();

        getSupportActionBar().setTitle("Sign Up"); // for set actionbar title
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);

        sup = (TextView) findViewById(R.id.sup);
        lin = (TextView) findViewById(R.id.lin);
        usrusr = (EditText) findViewById(R.id.usrusr);
        pswd = (EditText) findViewById(R.id.pswrdd);
        mail = (EditText) findViewById(R.id.mail);
        mophone = (EditText) findViewById(R.id.mobphone);
        Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/LatoLight.ttf");
        Typeface custom_font1 = Typeface.createFromAsset(getAssets(), "fonts/LatoRegular.ttf");
        mophone.setTypeface(custom_font);
        sup.setTypeface(custom_font1);
        pswd.setTypeface(custom_font);
        lin.setTypeface(custom_font);
        usrusr.setTypeface(custom_font);
        mail.setTypeface(custom_font);

    }

    public void signupButtonClicked(View view) {
        mAuth.createUserWithEmailAndPassword(mail.getText().toString(), pswd.getText().toString())
                .addOnCompleteListener(signup.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(signup.this, "New User Created successfully!", Toast.LENGTH_SHORT).show();
                            FirebaseUser user = mAuth.getCurrentUser();
                            Intent it = new Intent(signup.this,Login.class);
                            startActivity(it);
                        } else {
                            Toast.makeText(signup.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();

                        }
                    }
                });
    }

}

最佳答案

我有类似的问题,这个 post帮助我解决了我的问题。我忘记安装 Google Play 服务。

如果您在模拟器上运行它,请检查您是否已启用 Google Play 服务

Tools -> Android -> SDK Manager -> SDK Tools -> Google play services

安装后,重新启动 Android Studio 和模拟器

另一个重要的一点是,我必须将 com.google.gms:google-services: 从 4.1.0 降级到 4.0.0,这对我来说很有魅力。此帖here对此有帮助

dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
        classpath 'com.google.gms:google-services:4.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

关于java - 默认FirebaseApp在此过程中未初始化official96.yohan.lms。确保首先调用 FirebaseApp.initializeApp(Context),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54264483/

相关文章:

java - Java 8中basic和url base64编码的区别

node.js - Firebase 的云功能 - getaddrinfo ENOTFOUND

java - 考虑在配置中定义一个类型为 'com.test.project.repositories.TaskRepository' 的 bean @Repository 注释已经存在

android - 使用 git 子模块的多个支持库冲突

java - 用于不止一种类类型的 ArrayAdapter

java - 在 Java Android 中加载外部 JSON 文件

javascript - 有时,当我刷新页面时,firebase.auth().onAuthStateChanged 会返回 null 用户

javascript - 身份验证在 ionic 和 Firebase 中始终处于事件状态

java - 如何在单独的Java包中读取文本文件的内容

java - 获取原始 HTTP 响应 header