2017-07-18 72 views
0

Android Vitals收集的新故障部分和ANR之後,啓用persistence後,我有幾個IllegalStateExceptionfirebase databaseIllegalStateException在啓用Firebase數據庫中的持久性時

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) 

更新2018年2月3日

根據@Frank麪包車Puffelen的我創造了這個answer的建議,但仍然出現問題。

enter image description here

今天9:14應用版本27 LGE LG K8(mm1v),1536MB內存,安卓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) 
+2

參見[火力地堡的數據庫持久不工作#15](https://github.com/firebase/quickstart-android/issues/15) –

+0

搜索您的代碼其他對'setPersistenceEnable()'的調用。如果崩潰是'Configuration.onCreate()'中調用的結果,那麼我希望看到堆棧跟蹤中與創建應用程序相關的行,例如'android.app.Instrumentation.callApplicationOnCreate'。相反,有一些行提示系統正在嘗試創建「MyActivity」實例。 –

+0

@языкK - 我已經從應用程序類中刪除了代碼並使用瞭解決方案的建議代碼,但它沒有解決問題,它仍然生成錯誤日誌。 – k4k4sh1

回答

0

爲了解決這個問題,請使用代碼如下:

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

您需要使用此代碼作爲您的onCreate方法的第一個代碼。在這種情況下,setPersistenceEnabled()方法只會在以前未被調用時調用,因爲存在calledAlready布爾值。

希望它有幫助。

+1

最有可能的問題是由一個多進程的應用程序引起的,這是K鏈接到的東西。在這種情況下,這個代碼不會有幫助,因爲靜態布爾值鏈接到單個流程實例。內容提供者是唯一適當的解決方案,除了在你的主要活動中調用'setPersistenceEnabled(true)'(這意味着它被多次調用,但沒關係)。 –

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>