在android中保存到本地数据库和Firebase

时间:2016-12-05 09:12:41

标签: android sqlite firebase firebase-realtime-database

我正在开发和Android应用程序,并希望保存到本地SQLite数据库,然后希望它与Firebase同步,我已经使Firebase同步工作我只有问题保存到本地数据库,应用程序崩溃时我点击保存,但数据存储在firebase中,但不存储在本地SQLite中。如果有人可以提供帮助,我已将我的代码包括在内。

 buttonSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Creating firebase object
            Firebase ref = new Firebase(Config.FIREBASE_URL);
            ref.keepSynced(true);

            //Getting values to store
            String name = editTextName.getText().toString().trim();
            String address = editTextAddress.getText().toString().trim();
            String venue = editVenue.getText().toString().trim();
            String DateTime = editDateTime.getText().toString().trim();

            //Creating Person object
            final MeetingUser person = new MeetingUser();

            //Adding values
            person.setName(name);
            person.setAddress(address);
            person.setVenue(venue);
            person.setDateTime(DateTime);

            //Storing values to firebase
            ref.push().setValue(person);

            //Write to local SQL database
            boolean isInserted = DB.SaveUserMeetings(editTextName.getText().toString(),
                    editDateTime.getText().toString(),
                    editVenue.getText().toString(),
                    editTextAddress.getText().toString());
            if (isInserted == true)
                Toast.makeText(Capture_Meetings.this, "Data Not Saved", Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(Capture_Meetings.this, "Data Saved", Toast.LENGTH_SHORT).show();


            //Value event listener for realtime data update
            ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot snapshot) {
                    for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                        //Getting the data from snapshot
                        MeetingUser person = postSnapshot.getValue(MeetingUser.class);

                        //Adding it to a string
                        String string = "Name of School: " + person.getName() + "\n Date:" + person.getDateTime() + "\n Venue:" + person.getVenue() + "\nMeeting Notes: " + person.getAddress() + "\n\n";

                        //Displaying it on textview
                        textViewPersons.setText(string);
                    }
                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {
                    System.out.println("The read failed: " + firebaseError.getMessage());
                }
            });

        }
    });
}

我的错误日志

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.pooveshin.saica_sgb, PID: 10770
              java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.example.pooveshin.saica_sgb.DatabaseHelper.SaveUserMeetings(java.lang.String, java.lang.String, java.lang.String, java.lang.String)' on a null object reference
                  at com.example.pooveshin.saica_sgb.Capture_Meetings$3.onClick(Capture_Meetings.java:114)
                  at android.view.View.performClick(View.java:4780)
                  at android.view.View$PerformClick.run(View.java:19866)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5254)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

1 个答案:

答案 0 :(得分:1)

您是否考虑通过调用以下

启用Firebase离线功能(https://firebase.google.com/docs/database/android/offline-capabilities)...

FirebaseDatabase.getInstance().setPersistenceEnabled(true);