本文介绍了setPersistenceEnabled(true)崩溃的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在创建我的第一个Firebase应用其中一个要求是当网络不可用时运行。 Firebase指南指出:启用磁盘持久性允许我们的应用程序即使在重新启动应用程序后也保持其所有状态。我们可以用一行代码启用磁盘持久性。 FirebaseDatabase.getInstance()。setPersistenceEnabled(true); 在启用磁盘持久化的情况下,我们的同步数据和写入操作将在应用程序重新启动我们的应用程序应该能够在离线情况下无缝地工作。 另一个要求是使用Google登录。所以在我的 MainActivity 中检查用户是否登录,如果没有,我启动 SignInActivity 。 ( SignInActivity 来自Firebase示例。) SignInActivity 起作用,用户登录, MainActivity 再次启动。现在我的应用程序在代码行 FirebaseDatabase.getInstance()。setPersistenceEnabled(true); 中崩溃,消息如下: 调用setPersistenceEnabled()必须在FirebaseDatabase实例的任何其他用法之前创建。 现在,如果我重新启动我的应用程序,用户登录, SignInActivity 未启动,我的应用程序运行正常。 我在用户登录后如何避免这种崩溃的任何建议? 当我发布这个问题,我收到了一个建议,重定位 FirebaseDatabase。 getInstance()。setPersistenceEnabled(true); 到我的应用程序类。我得到完全相同的结果... SignInActivity 开始,完成,并且在 setPersistenceEnabled 中崩溃。 以下是我的MainActivity onCreate : @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); //调用setPersistenceEnabled()必须在FirebaseDatabase实例的任何其他用法之前进行。 //从SignInActivity返回时在这里崩溃。 FirebaseDatabase.getInstance()。setPersistenceEnabled(true); mFirebaseDbReference = FirebaseDatabase.getInstance()。getReference(); //初始化Firebase身份验证 mFirebaseAuth = FirebaseAuth.getInstance(); mFirebaseUser = mFirebaseAuth.getCurrentUser(); if(mFirebaseUser == null){ //未登录,启动登录活动 Timber.tag(MainActivity)。i(onCreate():User not signed在,启动SignInActivity); startActivity(new Intent(this,SignInActivity.class)); finish(); } else { mUsername = mFirebaseUser.getDisplayName(); Timber.tag(MainActivity)。i(onCreate():User \%s\),mUsername); if(mFirebaseUser.getPhotoUrl()!= null){ mPhotoUrl = mFirebaseUser.getPhotoUrl()。toString(); $解析方案 A FirebaseApp由ContentProvider进行初始化,因此在调用onCreate()时不会初始化。 获取Firebase数据库,如下所示: public class Utils { private static FirebaseDatabase mDatabase; $ b $ public static FirebaseDatabase getDatabase(){ if(mDatabase == null){ mDatabase = FirebaseDatabase.getInstance(); mDatabase.setPersistenceEnabled(true); } 返回mDatabase; 然后调用Utils.getDatabase()从任何Activity到使用数据库。 不要忘记把 compile'com.google.firebase:firebase-database:...'在build.gradle I’m creating my first Firebase App. One of its requirements is that it run when the network is not available. The Firebase guide states: "Enabling disk persistence allows our app to also keep all of its state even after an app restart. We can enable disk persistence with just one line of code.FirebaseDatabase.getInstance().setPersistenceEnabled(true);With disk persistence enabled, our synced data and writes will be persisted to disk across app restarts and our app should work seamlessly in offline situations."Another requirement is to use Google Sign In. So in my MainActivity I check if the User is signed in, if not, I launch the SignInActivity. (The SignInActivity is from the Firebase examples.) The SignInActivity works, the user gets logged in, and MainActivity is launched for a second time. Now my app crashes on the code line FirebaseDatabase.getInstance().setPersistenceEnabled(true); with the following message: Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance.Now if I restart my app, the User is signed in, the SignInActivity is not launched, my app runs fine.Any suggestions of how I avoid this crash after the User signs in?As I was posting this question, I received a suggestion to relocate FirebaseDatabase.getInstance().setPersistenceEnabled(true);to my "Application class". I get exactly the same result … SignInActivity starts, completes, and I get a crash on the setPersistenceEnabled.Below is my MainActivity onCreate:@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance. // Crash here upon returning from SignInActivity. FirebaseDatabase.getInstance().setPersistenceEnabled(true); mFirebaseDbReference = FirebaseDatabase.getInstance().getReference(); // Initialize Firebase Auth mFirebaseAuth = FirebaseAuth.getInstance(); mFirebaseUser = mFirebaseAuth.getCurrentUser(); if (mFirebaseUser == null) { // Not signed in, launch the Sign In activity Timber.tag("MainActivity").i("onCreate(): User not signed in, launching SignInActivity"); startActivity(new Intent(this, SignInActivity.class)); finish(); } else { mUsername = mFirebaseUser.getDisplayName(); Timber.tag("MainActivity").i("onCreate(): User \"%s\" signed in.", mUsername); if (mFirebaseUser.getPhotoUrl() != null) { mPhotoUrl = mFirebaseUser.getPhotoUrl().toString(); } } 解决方案 A FirebaseApp is initialized by a ContentProvider so it is not initialized at the time onCreate() is called.Get your FirebaseDatabase like this:public class Utils {private static FirebaseDatabase mDatabase;public static FirebaseDatabase getDatabase() { if (mDatabase == null) { mDatabase = FirebaseDatabase.getInstance(); mDatabase.setPersistenceEnabled(true); }return mDatabase;}}Then call Utils.getDatabase() from any Activity to use the Database.Don't forget to put compile 'com.google.firebase:firebase-database:...' in the build.gradle 这篇关于setPersistenceEnabled(true)崩溃的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-18 18:00