【问题标题】:Default FirebaseApp is no initialized in process (Error)默认 FirebaseApp 未在进程中初始化(错误)
【发布时间】:2019-04-16 07:55:33
【问题描述】:

我正在开发一个 firebase 应用程序,我只需将内容保存到 firebase 中的实时数据库即可。我在 logcat 中说有一个错误

原因:java.lang.IllegalStateException:默认 FirebaseApp 未在此进程 com.example.myfirebase 中初始化。请务必先调用 FirebaseApp.initializeApp(Context)。

即使在初始化 FirebaseApp 之后,我的应用也会崩溃。请帮帮我。

package com.example.myfirebase;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.firebase.FirebaseApp;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity {

    private EditText name,email;
    private Button save;

    DatabaseReference databaseReference;

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

        name = (EditText) findViewById(R.id.editText3);
        email = (EditText) findViewById(R.id.editText4);
        save = (Button) findViewById(R.id.button);

        FirebaseApp.initializeApp(this);

        databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");


        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                AddData();
            }
        });
    }

    public void AddData(){
        String Name = name.getText().toString().trim();
        String Email = email.getText().toString().trim();

        SaveData saveData = new SaveData(Name,Email);

        databaseReference.setValue(saveData);
    }

}

我想将内容保存到数据库中。

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    请尝试:

    在您的应用程序类中添加FirebaseApp.initializeApp(this);,而不是在Activity中。

    创建类扩展Application

    public class MyApplication extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            FirebaseApp.initializeApp(this);
        }
    }
    

    在 Android 清单中:

    <application
        android:name=".MyApplication">
    </application>
    

    【讨论】:

      【解决方案2】:

      更新你的项目级 gradle 文件类路径:-

      classpath 'com.google.gms:google-services:4.2.0'
      

      【讨论】:

      • 如果您有任何疑问,请发表评论,如果解决了您的问题,请提供答案
      【解决方案3】:

      您是否正在使用没有 Google Play 服务的设备,或者没有将 com.google.gms:google-services 添加到您的项目中?

      This page 似乎表明如果您忽略 Google Play 服务,您会在 Flutter 应用程序中遇到该错误,因此对您来说可能是一样的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-05
        • 2020-08-16
        • 1970-01-01
        • 2017-10-25
        • 1970-01-01
        • 2022-10-05
        • 2021-01-30
        • 2019-02-06
        相关资源
        最近更新 更多