简体   繁体   English

从 google firestore -Quiz 应用程序获取问题时应用程序崩溃

[英]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.

相关问题 Flutter 应用程序在云 Firestore 上执行并发事务时崩溃 - Flutter app crashes on concurrent transactions performed on cloud Firestore 我从 firestore 获取数据,但它没有出现在我的反应本机应用程序屏幕中 - I fetch data from firestore but it's not appearing in my react native app screen Next.js 应用程序在尝试实现谷歌 firebase 身份验证时崩溃 - Next.js app crashes when attempting to implement google firebase authentication 当大量文档从 Firestore 快照到达时,如何不让 ReactJS 应用程序冻结? - How not to make a ReactJS app freeze when a huge number of docs arrive from a Firestore snapshot? Flutter 应用程序终止时在后台获取 API - Flutter Fetch API in background when App is terminated 从 App Engine 连接到 Google Cloud SQL 时出现 ENOENT - ENOENT when connecting to Google Cloud SQL from App Engine 从 Firestore 获取单个文档 - Fetch Single Document from Firestore 运行集成了 Firestore 的应用程序时,“由于错误 9,GooglePlayServices 不可用” - "GooglePlayServices not available due to error 9" when running an app with Firestore integrated 当 iOS 应用程序在后台时,您可以收听 Firestore 更新吗? - Can you listen to Firestore updates when iOS app is in the background? Firebase android 中与 firestore SDK 一起使用时的应用程序检查配额限制 - Firebase App Check Quota Limits when using with firestore SDK in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM