0

My app keeps crashing when it sets the persistence-enable. If the parent field is found in Firebase it crashes, if not, it loads the layout properly. Then on storing data on firebase it crashes again as now the parent is found in the database.

 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");
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441

1 Answers1

0

First of all you create a class for application like as

public class MApplication extends Application {

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

In Manifest.xml, set the application name as MApplication:

 <application
    android:name="com.MyApplication"
    android:allowBackup="false"
    android:icon="@mipmap/logo"
 android:supportsRtl="true"
    android:theme="@style/AppTheme">
Binesh Kumar
  • 2,763
  • 1
  • 16
  • 23