I'm running into an exception when I try to findViewByID for any of the views in this Activity. It all looks quite normal to me so I'm not sure where I've gone wrong.
This portion makes me think I've imported the wrong packages or have something messed up in my gradle file?
Caused by: java.lang.ClassCastException: com.google.android.material.floatingactionbutton.FloatingActionButton cannot be cast to android.content.Context
Here's the activity:
import android.content.SharedPreferences
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.floatingactionbutton.FloatingActionButton
class EditingActivity : AppCompatActivity() {
private lateinit var sharedPref: SharedPreferences
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_editing)
setSupportActionBar(findViewById(R.id.toolbar))
val checkmark = FloatingActionButton(findViewById(R.id.checkmarkButton))
// val categoryView = EditText(findViewById(R.id.editTextCategory))
// val questionView = EditText(findViewById(R.id.editTextQuestion))
// val solutionView = EditText(findViewById(R.id.editTextSolution))
//
// sharedPref = getSharedPreferences("preferences_file", Context.MODE_PRIVATE)
// categoryView.setText(sharedPref.getString("category", " "))
// checkmark.setOnClickListener{
// //dialog
// }
}
@Override
override fun onCreateOptionsMenu(menu: Menu): Boolean {
getMenuInflater().inflate(R.menu.main_menu, menu)
return true
}
@Override
public override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle item selection
return true
}}
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=".EditingActivity">
<EditText
android:id="@ id/editTextQuestion"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/editTextCategory"
app:layout_constraintVertical_bias="0.0" />
<androidx.appcompat.widget.Toolbar
android:id="@ id/toolbar"
android:layout_width="409dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="@ id/editTextCategory"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPersonName"
android:minHeight="48dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/toolbar"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="@ id/editTextSolution"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/editTextQuestion"
app:layout_constraintVertical_bias="0.0" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@ id/checkmarkButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/editTextSolution"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@drawable/ic_baseline_check_circle_24"
android:contentDescription="Done"
android:focusable="true" />
</androidx.constraintlayout.widget.ConstraintLayout>
Error messages -- this is what I get for findViewByID for the floating action button, but I'll get a similar error for any of the editTexts.
2022-10-03 19:24:43.861 5011-5011/weiss.kotlin.flashcardapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: weiss.kotlin.flashcardapp, PID: 5011
java.lang.RuntimeException: Unable to start activity ComponentInfo{weiss.kotlin.flashcardapp/weiss.kotlin.flashcardapp.EditingActivity}: java.lang.ClassCastException: com.google.android.material.floatingactionbutton.FloatingActionButton cannot be cast to android.content.Context
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3685)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3842)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2252)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7842)
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)
Caused by: java.lang.ClassCastException: com.google.android.material.floatingactionbutton.FloatingActionButton cannot be cast to android.content.Context
at weiss.kotlin.flashcardapp.EditingActivity.onCreate(EditingActivity.kt:23)
at android.app.Activity.performCreate(Activity.java:8054)
at android.app.Activity.performCreate(Activity.java:8034)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1341)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3666)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3842)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2252)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7842)
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)
CodePudding user response:
problem is here
val checkmark = FloatingActionButton(findViewById(R.id.checkmarkButton))
It should be :
val checkmark: FloatingActionButton = findViewById(R.id.checkmarkButton)
//or
//val checkmark = findViewById<FloatingActionButton>(R.id.checkmarkButton)
CodePudding user response:
val checkmark: FloatingActionButton = findViewById(R.id.checkmarkButton)
When you are trying to get the view by id you have to explicitly define the type.
In your implementation, you are calling the class and passing the view id to it. Which is expecting the context.