因此,我只是尝试使用Firebase从实时数据库更新活动的工具栏标签。我收到以下错误:
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)
即使在调用数据库实例上的任何其他方法之前使用setPersistenceEnabled(),我仍然会遇到此错误。这是我的Activity.java代码:
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) {
}
});
// ---------------------------------------------------------------------------
是什么导致崩溃。我在其他两个活动上使用的是完全相同的代码段...
最佳答案
这部分mFirebaseInstance.setPersistenceEnabled(true);
应该只在第一个活动中。不应多次调用它。
更好的解决方案是将该行放入Application类的onCreate方法中。您可以here了解更多信息。
关于android - Android Firebase-setPersistenceEnabled(true)使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41250507/