Android Firebase – setPersistenceEnabled (true) blocking application

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/com.xlr8labs.FirebaseAuth E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.xlr8labs.FirebaseAuth, PID: 10295
                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xlr8labs.FirebaseAuth/com.xlr8labs.FirebaseAuth.RegisterActivity}: com.google.firebase.database.DatabaseException: Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance.
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2341)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2393)
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                           at android.os.Looper.loop(Looper.java:193)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5295)
                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:515)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
                                                                           at dalvik.system.NativeStart.main(Native Method)
                                                                        Caused by: com.google.firebase.database.DatabaseException: Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance.
                                                                           at com.google.firebase.database.FirebaseDatabase.zzhM(Unknown Source)
                                                                           at com.google.firebase.database.FirebaseDatabase.setPersistenceEnabled(Unknown Source)
                                                                           at com.xlr8labs.FirebaseAuth.RegisterActivity.onCreate(RegisterActivity.java:35)
                                                                           at android.app.Activity.performCreate(Activity.java:5264)
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2393)
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                           at android.os.Looper.loop(Looper.java:193)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5295)
                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:515)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
                                                                           at dalvik.system.NativeStart.main(Native Method)

Even though I’m using setPersistenceEnabled() before calling any other methods on my database instance, I’m still getting this error. Here is my Activity.java code:

public class RegisterActivity extends AppCompatActivity {
 private FirebaseDatabase mFirebaseInstance;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        // ------------------ Update activity label from database in realtime ------
        mFirebaseInstance = FirebaseDatabase.getInstance();
        mFirebaseInstance.setPersistenceEnabled(true);
        mFirebaseInstance.getReference("RegisterLabel").keepSynced(true);
        mFirebaseInstance.getReference("RegisterLabel").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Log.e(LOG_TAG,"Register label changed");
                String appTitle = dataSnapshot.getValue().toString();
                getSupportActionBar().setTitle(appTitle);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        // ---------------------------------------------------------------------------

What is causing the crash. I’m using the exact same snippet on two other activities…

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
.

Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Android Firebase – setPersistenceEnabled (true) blocking application

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录