关于在Firebase数据库中启用持久性的IllegalStateException

问题描述 投票:0回答:2

在从Android Vitals收集的失败和ANR的新部分之后,我有几个IllegalStateExceptionfirebase database启用persistence

public class Configuration extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        if(!FirebaseApp.getApps(this).isEmpty()) {
            FirebaseDatabase.getInstance().setPersistenceEnable(true);
        } 
    }

}

例外:

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
  at android.app.ActivityThread.access$900(ActivityThread.java:157)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:148)
  at android.app.ActivityThread.main(ActivityThread.java:5530)
  at java.lang.reflect.Method.invoke(Method.java:0)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:733)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:623)
Caused by: java.lang.IllegalStateException: 
  at com.google.firebase.FirebaseApp.getInstance(FirebaseApp.java:0)
  at com.google.firebase.database.FirebaseDatabase.getInstance(FirebaseDatabase.java:0)
  at <OR>.getInstance(FirebaseDatabase.java:0)
  at <OR>.setPersistenceEnabled(FirebaseDatabase.java:0)
  at <OR>.zziE(FirebaseDatabase.java:0)
  at myapp.MyActivity.<init>(MyActivity.java:0)
  at java.lang.Class.newInstance(Class.java:0)
  at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2329)

更新02/03/2018

根据@Frank van Puffelen的建议,我创建了这个answer,但问题仍然存在。

enter image description here

今天上午9:14应用版本27 LGE LG K8(mm1v),1536MB RAM,Android 6.0报告1

java.lang.RuntimeException:      
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2339)     
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2494)     
  at android.app.ActivityThread.access$900 (ActivityThread.java:157)     
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1356)     
  at android.os.Handler.dispatchMessage (Handler.java:102)     
  at android.os.Looper.loop (Looper.java:148)     
  at android.app.ActivityThread.main (ActivityThread.java:5551)     
  at java.lang.reflect.Method.invoke (Method.java)     
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:731)     
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:621)
Caused by: java.lang.IllegalStateException:      
  at com.google.firebase.FirebaseApp.getInstance (FirebaseApp.java)     
  at com.google.firebase.database.FirebaseDatabase.getInstance (FirebaseDatabase.java)
  or                     .setPersistenceEnabled (FirebaseDatabase.java)
  or                     .zzph (FirebaseDatabase.java)     
  at myapp.MyActivity.<init> (MyActivity.java)     
  at java.lang.Class.newInstance (Class.java)     
  at android.app.Instrumentation.newActivity (Instrumentation.java:1068)     
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2329)     
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2494)     
  at android.app.ActivityThread.access$900 (ActivityThread.java:157)     
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1356)     
  at android.os.Handler.dispatchMessage (Handler.java:102)     
  at android.os.Looper.loop (Looper.java:148)     
  at android.app.ActivityThread.main (ActivityThread.java:5551)     
  at java.lang.reflect.Method.invoke (Method.java)     
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:731) 
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:621)
android firebase firebase-realtime-database
2个回答
1
投票
public class FirebaseContentProvider extends ContentProvider {

    private static FirebaseDatabase mDatabase;

    @Override
    public boolean onCreate() {

        if (mDatabase == null) {
            mDatabase = FirebaseDatabase.getInstance();
            mDatabase.setPersistenceEnabled(true);
        }

        return true;
    }
}

AndroidManifest.xml中

<application
 ... >
    <provider
         android:name=".test.FirebaseContentProvider"
         android:authorities="com.test.FirebaseContentProvider"
         android:exported="false" />
</application>

0
投票

要解决此问题,请使用以下代码:

private static boolean calledAlready = false;
if (!calledAlready) {
    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    calledAlready = true;
}

您需要使用此代码作为onCreate方法的第一个代码。在这种情况下,setPersistenceEnabled()方法只有在之前未被调用时才会被调用,因为存在calledAlready布尔值。

希望能帮助到你。

© www.soinside.com 2019 - 2024. All rights reserved.