【问题标题】:FirebaseDatabase.getInstance().setPersistenceEnabled(true) crashes my appFirebaseDatabase.getInstance().setPersistenceEnabled(true) 使我的应用程序崩溃
【发布时间】:2018-03-19 02:01:43
【问题描述】:

我的应用在设置持久性启用时不断崩溃。 如果在 Firebase 中找到父字段,它会崩溃,如果没有,它会正确加载布局。 然后在 firebase 上存储数据时,它再次崩溃,因为现在在数据库中找到了父级。

 protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat_line);

    final DatabaseReference reference1, reference2;

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

    layout = (LinearLayout)findViewById(R.id.layout1);
    sendButton = (ImageView)findViewById(R.id.sendButton);
    messageArea = (EditText)findViewById(R.id.messageArea);
    scrollView = (ScrollView)findViewById(R.id.scrollView);

        reference1 = FirebaseDatabase.getInstance().getReference("message/" + "username" + "_" + "admin");
        reference2 = FirebaseDatabase.getInstance().getReference("message/" + "admin" + "_" + "username");

【问题讨论】:

  • 把 FirebaseDatabase.getInstance().setPersistenceEnabled(true);在应用程序文件中。
  • 如果您看到崩溃,请在您的问题中包含堆栈跟踪。

标签: android firebase firebase-realtime-database


【解决方案1】:

首先你为应用程序创建一个类

public class MApplication extends Application {

@Override
public void onCreate() {
    super.onCreate();
    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
}}

在Manifest.xml中,设置应用名称为MApplication:

 <application
    android:name="com.MyApplication"
    android:allowBackup="false"
    android:icon="@mipmap/logo"
 android:supportsRtl="true"
    android:theme="@style/AppTheme">

【讨论】:

    猜你喜欢
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    相关资源
    最近更新 更多