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.
CodePudding user response:
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.