gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:51:22 25 4
gpt4 key购买 nike

我正在开发 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)

最佳答案

您是否考虑过使用 Firebase 离线功能 (https://firebase.google.com/docs/database/android/offline-capabilities) ...通过调用以下方式启用

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

关于android - 在android中保存到本地数据库和Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40970687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com