whats the error in the following code of android studio

Nimesh Garg :

This part of my program is to add the current time to my cloud firestore. The app works except this part of the program. Whenever I try to run this part of the program, the app stops running and there is no change in my database.

 mreport.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String tower = mtower.getSelectedItem().toString();
                        String currentTime = new SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(new Date());
                        String currentDate = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date());

                        CollectionReference dbr = db.collection("tower")
                                .document(tower).collection(currentDate)
                                .document("duty").collection("off");
                                dbr.add(currentTime)
                                .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                                    @Override
                                    public void onSuccess(DocumentReference documentReference) {
                                        Toast.makeText(MainActivity.this,"REPORTED SUCCESSFULLY" , Toast.LENGTH_SHORT).show();
                                    }
                                })
                                .addOnFailureListener(new OnFailureListener() {
                                    @Override
                                    public void onFailure(@NonNull Exception e) {
                                        Toast.makeText(MainActivity.this, "EORROR!",Toast.LENGTH_SHORT).show();
                                    }
                                });
                    }
                });

E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f1108012880 D/OpenGLRenderer: endAllStagingAnimators on 0x7f10fc596c00 (DropDownListView) with handle 0x7f110b0fcce0 D/EGL_emulation: eglMakeCurrent: 0x7f10fd15db60: ver 3 1 (tinfo 0x7f11078e2ba0) D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.guardchecker, PID: 3684 java.lang.IllegalArgumentException: Invalid data. Data must be a Map or a suitable POJO object, but it was of type: java.lang.String at com.google.firebase.firestore.UserDataReader.convertAndParseDocumentData(com.google.firebase:firebase-firestore@@21.4.1:234) at com.google.firebase.firestore.UserDataReader.parseSetData(com.google.firebase:firebase-firestore@@21.4.1:75) at com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore@@21.4.1:166) at com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore@@21.4.1:146) at com.google.firebase.firestore.CollectionReference.add(com.google.firebase:firebase-firestore@@21.4.1:121) at com.example.guardchecker.MainActivity$1.onClick(MainActivity.java:67) at android.view.View.performClick(View.java:5198) at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) W/DynamiteModule: Local module descriptor class for providerinstaller not found.

Frank van Puffelen :

Firestore is a collection/document database, where you can only store a value in a field in a document. The problem is that you're trying to store a single value in a Cloud Firestore document, but a document must always have key-value pairs.

So you'll need to determine what field you want to store currentTime in and then pass a key-value pair. E.g.

Map<String, Object> values = new HashMap<>(); 
values.put("time", currentTime); 
dbr.add(values); 

In the above fragment, you'll see that we put the value you want to store in a map with a key time and then store that key-value pair in the database.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=345801&siteId=1