[英]App crashes when fetch questions from google firestore -Quiz app
I developed a quiz app for android. I use google firestore to store my question.我为 android 开发了一个测验应用程序。我使用 google firestore 来存储我的问题。 I use following code to fetch the questions from the firestore.
我使用以下代码从 firestore 中获取问题。 In this code, first set of questions retrieve finely.
在这段代码中,第一组问题进行了精细检索。 but when I open another question collection("SET"), app crashes.
但是当我打开另一个问题集合(“SET”)时,应用程序崩溃了。 how to fetch sets without crashing the app.
如何在不使应用程序崩溃的情况下获取集合。 please help.
请帮忙。
private void getQuestionList() {
questionList.clear();
firestore.collection("QUIZ").document("CAT" + String.valueOf(category_id)).collection("SET" + String.valueOf(setNo))
.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()){
QuerySnapshot questions = task.getResult();
for (QueryDocumentSnapshot doc : questions){
questionList.add(new Question(doc.getString("QUESTION"),
doc.getString("A"),
doc.getString("B"),
doc.getString("C"),
doc.getString("D"),
Integer.valueOf(doc.getString("ANSWER"))
));
} setQuestion();
}else {
Toast.makeText(QuestionsActivity.this,task.getException().getMessage(),Toast.LENGTH_SHORT).show();
}
}
});
}
Firestore database structure Firestore 数据库结构
Logcat message日志消息
07-30 13:22:51.835 28139-28139/com.supun.mcqapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.supun.mcqapp, PID: 28139
java.lang.RuntimeException: Field 'ANSWER' is not a java.lang.String
at com.google.firebase.firestore.DocumentSnapshot.castTypedValue(DocumentSnapshot.java:499)
at com.google.firebase.firestore.DocumentSnapshot.getTypedValue(DocumentSnapshot.java:491)
at com.google.firebase.firestore.DocumentSnapshot.getString(DocumentSnapshot.java:361)
at com.supun.mcqapp.QuestionsActivity$1.onComplete(QuestionsActivity.java:102)
at com.google.android.gms.tasks.zzj.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:819)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:210)
at android.app.ActivityThread.main(ActivityThread.java:5982)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)
07-30 13:22:51.985 2369-22884/? E/SQLiteDatabase: Error inserting source=16 tag=Measurement.PackageMeasurementTaskService.UPLOAD_TASK_TAG requires_charging=0 target_package=com.google.android.gms source_version=19831000 required_network_type=0 flex_time=2183000 target_class=com.google.android.gms.measurement.PackageMeasurementTaskService runtime=1627631571923 retry_strategy={"maximum_backoff_seconds":{"3600":0},"initial_backoff_seconds":{"30":0},"retry_policy":{"0":0}} last_runtime=0 period=4368000 task_type=0 job_id=-1 user_id=0
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: pending_ops.tag, pending_ops.target_class, pending_ops.target_package, pending_ops.user_id (Sqlite code 2067), (OS error - 2:No such file or directory)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:799)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1536)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1401)
at aqnp.a(:com.google.android.gms@19831020@19.8.31 (040406-284611645):162)
at aqne.a(:com.google.android.gms@19831020@19.8.31 (040406-284611645):163)
at aqne.a(:com.google.android.gms@19831020@19.8.31 (040406-284611645):24)
at aqne.a(:com.google.android.gms@19831020@19.8.31 (040406-284611645):156)
at aqjx.run(:com.google.android.gms@19831020@19.8.31 (040406-284611645):8)
at tfd.b(:com.google.android.gms@19831020@19.8.31 (040406-284611645):15)
at tfd.run(:com.google.android.gms@19831020@19.8.31 (040406-284611645):10)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at tld.run(:com.google.android.gms@19831020@19.8.31 (040406-284611645))
at java.lang.Thread.run(Thread.java:833)
07-30 13:22:51.990 2369-22884/? E/SQLiteDatabase: Error inserting source=16 tag=Measurement.PackageMeasurementTaskService.UPLOAD_TASK_TAG requires_charging=0 target_package=com.google.android.gms source_version=19831000 required_network_type=0 flex_time=2183000 target_class=com.google.android.gms.measurement.PackageMeasurementTaskService runtime=1627631571960 retry_strategy={"maximum_backoff_seconds":{"3600":0},"initial_backoff_seconds":{"30":0},"retry_policy":{"0":0}} last_runtime=0 period=4368000 task_type=0 job_id=-1 user_id=0
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: pending_ops.tag, pending_ops.target_class, pending_ops.target_package, pending_ops.user_id (Sqlite code 2067), (OS error - 2:No such file or directory)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:799)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1536)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1401)
at aqnp.a(:com.google.android.gms@19831020@19.8.31 (040406-284611645):162)
at aqne.a(:com.google.android.gms@19831020@19.8.31 (040406-284611645):163)
at aqne.a(:com.google.android.gms@19831020@19.8.31 (040406-284611645):24)
at aqne.a(:com.google.android.gms@19831020@19.8.31 (040406-284611645):156)
at aqjx.run(:com.google.android.gms@19831020@19.8.31 (040406-284611645):8)
at tfd.b(:com.google.android.gms@19831020@19.8.31 (040406-284611645):15)
at tfd.run(:com.google.android.gms@19831020@19.8.31 (040406-284611645):11)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at tld.run(:com.google.android.gms@19831020@19.8.31 (040406-284611645))
at java.lang.Thread.run(Thread.java:833)
Summery - logcat E/MultiWindowProxy: getServiceInstance failed!
Summery - logcat
E/MultiWindowProxy: getServiceInstance failed!
The problem is here Field 'ANSWER' is not a java.lang.String
.问题在这里
Field 'ANSWER' is not a java.lang.String
。
You need to check the type of 'ANSWER' in your for loop.您需要在 for 循环中检查“ANSWER”的类型。 ANSWER is not a string so you can't try to get it from
doc.getString("ANSWER")
ANSWER 不是字符串,因此您不能尝试从
doc.getString("ANSWER")
获取它
In your Firestore database
, make field "ANSWER" into String
.在您的
Firestore database
中,将字段“ANSWER”设为String
。 I think you gave the type of the "ANSWER" as Integer
.我认为您将“ANSWER”的类型指定为
Integer
。
And also make sure to check the spellings
in the all fields.还要确保检查所有字段中的
spellings
。 Sometimes it should be the fault.有时它应该是错误的。 Because I got that error too.
因为我也有这个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.