java.lang.IllegalStateException:名为[DEFAULT]的FirebaseApp

时间:2022-12-05 20:32:32

I have been getting this issue.. followed the upgrade guide for new firebase sdk...saved the google services json file in app directory.. still the same error as you but for the database...

我一直在收到这个问题..按照新firebase sdk的升级指南...保存了应用程序目录中的google服务json文件..仍然和你一样但是对于数据库...

Caused by: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist.

3 个解决方案

#1


32  

Are you using Firebase Crash Reporting? You might be hitting this error because of that if its hitting a background process and not the main.

您使用的是Firebase崩溃报告吗?如果它遇到后台进程而不是主进程,你可能会遇到这个错误。

Crash Reporting creates a second process (background_crash) to send crashes. Unfortunately, all processes in an Android app share a common Application subclass, so your onCreate method is run in the background process as well. That tries to initialise database, which fails.

崩溃报告创建第二个进程(background_crash)以发送崩溃。不幸的是,Android应用程序中的所有进程共享一个公共的Application子类,因此您的onCreate方法也在后台进程中运行。这会尝试初始化数据库,但失败了。

The fix is to make sure the Database call is only run when Firebase is properly configured (which will be in the main process). You can check like this:

修复方法是确保仅在正确配置Firebase时运行数据库调用(这将在主进程中)。你可以这样检查:

@Override
public void onCreate() {
  super.onCreate();
  if (!FirebaseApp.getApps(this).isEmpty()) {
    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
  }
}

#2


4  

I solved this error by don't put anything of Firebase in Application. I put it in to MainActivity. Example: MainActivity.java

我解决了这个错误,不要在应用程序中放入任何Firebase。我把它放在MainActivity中。示例:MainActivity.java

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    ...
}

-UPDATE

-UPDATE

Other solution is create a DatabaseHelper class contain one FirebaseDatabase instance.

其他解决方案是创建一个DatabaseHelper类,其中包含一个FirebaseDatabase实例。

public class DatabaseHelper {
  private static boolean persistenceEnable = false;
  private static FirebaseDatabase mDatabase;


  public static boolean isPersistenceEnable(){
    return persistenceEnable;
  }
  public static FirebaseDatabase getInstance() {
    if (mDatabase == null) {
      mDatabase = FirebaseDatabase.getInstance();
      if(persistenceEnable==true) {
        mDatabase.setPersistenceEnabled(true);
      }
    }

    return mDatabase;
  }
}

and using by: FirebaseDatabase database = DatabaseHelper.getInstance();

和使用:FirebaseDatabase数据库= DatabaseHelper.getInstance();

#3


0  

Extending @Ian Barber's solution, you can try this generic check to skip processing your custom Application.onCreate for all non-main processes. If the additional processes don't belong to you, then you don't want any of your custom to run.

扩展@Ian Barber的解决方案,您可以尝试此通用检查以跳过处理所有非主要进程的自定义Application.onCreate。如果其他进程不属于您,则您不希望运行任何自定义。

@Override
public void onCreate() {
    super.onCreate();
    if (FirebaseApp.getApps(this).isEmpty()) {
        // No firebase apps; we are in a non-main process
        // skip custom Application.onCreate
        return;
    }
    // Firebase init only in the main process
    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    // other code
}

#1


32  

Are you using Firebase Crash Reporting? You might be hitting this error because of that if its hitting a background process and not the main.

您使用的是Firebase崩溃报告吗?如果它遇到后台进程而不是主进程,你可能会遇到这个错误。

Crash Reporting creates a second process (background_crash) to send crashes. Unfortunately, all processes in an Android app share a common Application subclass, so your onCreate method is run in the background process as well. That tries to initialise database, which fails.

崩溃报告创建第二个进程(background_crash)以发送崩溃。不幸的是,Android应用程序中的所有进程共享一个公共的Application子类,因此您的onCreate方法也在后台进程中运行。这会尝试初始化数据库,但失败了。

The fix is to make sure the Database call is only run when Firebase is properly configured (which will be in the main process). You can check like this:

修复方法是确保仅在正确配置Firebase时运行数据库调用(这将在主进程中)。你可以这样检查:

@Override
public void onCreate() {
  super.onCreate();
  if (!FirebaseApp.getApps(this).isEmpty()) {
    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
  }
}

#2


4  

I solved this error by don't put anything of Firebase in Application. I put it in to MainActivity. Example: MainActivity.java

我解决了这个错误,不要在应用程序中放入任何Firebase。我把它放在MainActivity中。示例:MainActivity.java

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    ...
}

-UPDATE

-UPDATE

Other solution is create a DatabaseHelper class contain one FirebaseDatabase instance.

其他解决方案是创建一个DatabaseHelper类,其中包含一个FirebaseDatabase实例。

public class DatabaseHelper {
  private static boolean persistenceEnable = false;
  private static FirebaseDatabase mDatabase;


  public static boolean isPersistenceEnable(){
    return persistenceEnable;
  }
  public static FirebaseDatabase getInstance() {
    if (mDatabase == null) {
      mDatabase = FirebaseDatabase.getInstance();
      if(persistenceEnable==true) {
        mDatabase.setPersistenceEnabled(true);
      }
    }

    return mDatabase;
  }
}

and using by: FirebaseDatabase database = DatabaseHelper.getInstance();

和使用:FirebaseDatabase数据库= DatabaseHelper.getInstance();

#3


0  

Extending @Ian Barber's solution, you can try this generic check to skip processing your custom Application.onCreate for all non-main processes. If the additional processes don't belong to you, then you don't want any of your custom to run.

扩展@Ian Barber的解决方案,您可以尝试此通用检查以跳过处理所有非主要进程的自定义Application.onCreate。如果其他进程不属于您,则您不希望运行任何自定义。

@Override
public void onCreate() {
    super.onCreate();
    if (FirebaseApp.getApps(this).isEmpty()) {
        // No firebase apps; we are in a non-main process
        // skip custom Application.onCreate
        return;
    }
    // Firebase init only in the main process
    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    // other code
}