Application stops when add custom View component

Viewed 42

I tried to create a clock in Android Studio using Kotlin. I created a View with clock you can see code down.

class ClockView(c: Context, attrs: AttributeSet?, defStyleAttr: Int) : View(c, attrs, defStyleAttr) {
    private val paint = Paint()

    init {
        paint.color = Color.BLACK
        paint.style = Paint.Style.FILL
        paint.textSize = 60F
        paint.isAntiAlias = true
    }

    override fun onDraw(canvas: Canvas?) {
        val calendar = Calendar.getInstance()
        val hour: String = calendar.get(Calendar.HOUR_OF_DAY).toString()
        val minute: String = calendar.get(Calendar.MINUTE).toString()
        val second: String = calendar.get(Calendar.SECOND).toString()

        canvas?.drawColor(Color.WHITE)
        canvas?.drawText(hour, width / 2F - 200F, height / 2F, paint)
        canvas?.drawText(minute, width / 2F - 100F, height / 2F, paint)
        canvas?.drawText(second, width / 2F, height / 2F, paint)

        postInvalidateDelayed(500)
        invalidate()

        super.onDraw(canvas)
    }
}

And I implemented it to my MainActivity XML 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">

   <com.example.adnroid_clock.ClockView
       android:id="@+id/ClockView"
       android:layout_width="304dp"
       android:layout_height="243dp"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintHorizontal_bias="0.495"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toTopOf="parent"
       app:layout_constraintVertical_bias="0.157" />

</androidx.constraintlayout.widget.ConstraintLayout>

I launched application an got that error.Run log

1 Answers

The stack trace says that you use androidx.constraintlayout.Dwidget.ConstraintLayout (notice the D between the . and the widget) in some layout file. Presumably, that is your activity's layout file, given how the stack trace is set up.

Regardless, find the layout with this extra D, and remove the D.

Related