App crashes when I try to read data from Cloud Firestore

qwerty123456

I am new to Kotlin, and I was trying to insert and read data from Cloud Firestore with the following code:

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import com.google.firebase.firestore.FirebaseFirestore

class MainActivity : AppCompatActivity() {

    private val db = FirebaseFirestore.getInstance()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val insertButton = findViewById<Button>(R.id.insertButton)
        val getButton = findViewById<Button>(R.id.getButton)

        insertButton.setOnClickListener {
            db.collection("users").document("testUser").set(
                hashMapOf("name" to "test1", "username" to "test2")
            )
        }

        getButton.setOnClickListener {
            db.collection("users").document("testUser").get().addOnSuccessListener {
                findViewById<EditText>(R.id.test1TextView).setText(it.get("name") as String)
                findViewById<EditText>(R.id.test2TextView).setText(it.get("username") as         
                String)
            }
        }

    }
}

Layout file:

 <?xml version="1.0" encoding="utf-8"?>
 <androidx.constraintlayout.widget.ConstraintLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity">

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/test1TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test1" />

    <TextView
        android:id="@+id/test2TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test2" />

    <Button
        android:id="@+id/insertButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="insert" />

    <Button
        android:id="@+id/getButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Get" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

The app is simple, with two buttons, one to insert on the database and the other to read from it and change some TextViews with the read data. The app looks like this: https://i.stack.imgur.com/5VLXG.png

The "Insert" button works fine and creates the data on the Firestore properly (Firestore image: https://i.stack.imgur.com/anapG.png). The problem comes when the "Get" button is clicked and the app tries to read data from the Firestore, it simply crashes.

Logcat output:

--------- beginning of crash
2022-03-15 06:04:15.889 20433-20433/com.eugenisb.testapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.eugenisb.testapp, PID: 20433
java.lang.ClassCastException: com.google.android.material.textview.MaterialTextView cannot be cast to android.widget.EditText
    at com.eugenisb.testapp.MainActivity.onCreate$lambda-2$lambda-1(MainActivity.kt:28)
    at com.eugenisb.testapp.MainActivity.$r8$lambda$dpktUtUdZqv_kK0jh4DAEzfEDP0(Unknown Source:0)
    at com.eugenisb.testapp.MainActivity$$ExternalSyntheticLambda2.onSuccess(Unknown Source:4)
    at com.google.android.gms.tasks.zzm.run(com.google.android.gms:play-services-tasks@@18.0.1:1)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7839)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

I do not know if the solution to this is trivial, but I tried searching for a solution to this and did not find it.

Thank you for your answers.

lyncx

According to the crash logs you're using MaterialTextView rather than EditTextView. Change the type cast of findViewById<EditText>(R.id.test1TextView).setText(it.get("name") as String) to findViewById<MaterialTextView >(R.id.test1TextView).setText(it.get("name") as String) or what ever type of textview you are using. Perhaps show your xml layout to further confirm the type of view you are using.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Android:App crashes when i try to retrieve data from intent

App crashes when I try to insert data into database

App crashes when i try to add data to my database

Why can I save an enum to Firestore, but it crashes the app when I try to retrieve the same?

When I try to set a clickListener, app crashes

App crashes when I try to turn on Bluetooth

App crashes when I try to logout

Android app crashes when i try to reLogin

App crashes when i try to run this page

App crashes when I try to open a Fragment

App crashes when i try to set onClickListener

Flutter cloud_firestore(0.9.0) package crashes when opening the app

My app crashes when I try to add Image Button to it

When I Try to Change the Color of SnackBar TextView App Crashes

MapFragment make app crashes at runtime when I try to setMapType

my app crashes when I try to download an image

App crashes when I try to edit an EditText view

My app crashes when i try to move to a particular layout

I got an error when i try to update data from firestore "FirebaseError: Function CollectionReference.doc()"

Read data from Cloud firestore with Firebase cloud function

How come my DotNet Blazor WASM app fails to finish loading when I try to read some data from a local SQL database from WebAppBuilder in "program.cs'?

I can't store in cloud storage , I get acces denied when I try to export data from vm to my cloud storage

Cloud firestore crashes app in APK release mode

When I try to read a JSON data from a file (exported from xlsx) in python, it throws KeyError

How can I read date ( timestamp ) from Cloud-Firestore?

App Crashes when try to set the maximumValue to the UISlider

Using try/catch for preventing app from crashes

The app crashes when adding new keys in firestore

App crashes when saving a dictionary object in Firestore