Android Firebase - setPersistenceEnabled(true) crashing the app

前端 未结 5 1490
伪装坚强ぢ
伪装坚强ぢ 2021-01-25 01:00

So I was just trying out Firebase to update Toolbar label of activities from realtime database. I am getting the following error:

12-21 00:25:19.890 10295-10295/         


        
相关标签:
5条回答
  • 2021-01-25 01:38

    Remove the line mFirebaseInstance.setPersistenceEnabled(true); from your RegisterActivity class.

    Make a custom class which should extend Application and write it there.

    Example :

    public class JustExample extends Application {
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            if (!FirebaseApp.getApps(this).isEmpty())
            {
                FirebaseDatabase.getInstance().setPersistenceEnabled(true);
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-25 01:39
    boolean flag = true; //before onCreate() method
    
    //inside onStart() Method
    if(flag)
    { 
      firebaseDatabase.setPersistenceEnabled(true);
      flag = false;
    }
    
    0 讨论(0)
  • 2021-01-25 01:42

    This part mFirebaseInstance.setPersistenceEnabled(true); should be just in first Activity. It's shouldn't be called more than once.

    Better solution would be to put that line in onCreate method of your Application class. You can read more about it here.

    0 讨论(0)
  • 2021-01-25 01:43

    You just simply uninstall the previously installed app. And let it install the fresh app. It will resolve the problem.

    0 讨论(0)
  • 2021-01-25 01:59

    You can use a Model class and use Method inside it such as the one mentioned below.

    public class DatabaseUtility {
    
        private static FirebaseDatabase database;
    
        public static FirebaseDatabase getDatabase(){
            if(database==null){
                database=FirebaseDatabase.getInstance();
                database.setPersistenceEnabled(true);
            }
            return database;
        }
    }
    

    This will return database instance and you can use database.getReference(); method to get Root reference. This will also save some time if the database object is already present.

    0 讨论(0)
提交回复
热议问题