Search code examples
androidkotlinclasscastexceptionmillisecondschronometer

ClassCastException casting chronometer.java file


I'm using Kotlin. I tried using the Millisecond Chronometer found here https://github.com/antoniom/Millisecond-Chronometer. I downloaded the zipfile, and then copied the Chronometer.java file into a new .java file in my project.

I placed a chronometer widget in the layout file and gave its id chrono

When I tried to use Chronometer.java, it throws me an error:

val mChronometer = findViewById<Chronometer>(R.id.chrono)
//Caused by: java.lang.ClassCastException: android.widget.Chronometer cannot be cast to com.example.rinor.chronometertimer.Chronometer

How to fix this? I'm sure if I get this line right everything else is smooth sailing from there on.


Solution

  • Probably you use the wrong Chronometer in your .xml <Chronometer>...</Chronometer> instead of

    <com.example.rinor.chronometertimer.Chronometer
        android:layout_width="match_parent"
        android:layout_height="wrap_content">...
    </com.example.rinor.chronometertimer.Chronometer>