I’m trying to learn Kotlin in Android Studio 2023.3.1 by working my way through the examples in Android Programming, The Big Nerd Ranch, 5th Edition. The CriminalIntent example contains the class CrimeDetailFragment.kt, shown below:
package com.bignerdranch.android.criminalintent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.widget.doOnTextChanged
import androidx.fragment.app.Fragment
import com.bignerdranch.android.criminalintent.databinding.FragmentCrimeDetailBinding
import java.util.UUID
class CrimeDetailFragment : Fragment() {
private lateinit var binding: FragmentCrimeDetailBinding
private lateinit var crime: Crime
override fun oncreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
crime = Crime(
id = UUID.randomUUID(),
title = "",
date = java.util.Date(),
isSolved = false
)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
) : View? {
binding = FragmentCrimeDetailBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.apply {
crimeTitle.doOnTextChanged { text, _, _, _ ->
crime = crime.copy(title = text.toString())
}
crimeDate.apply {
text = crime.date.toString()
isEnabled = false
}
crimeSolved.setOnCheckedChangeListener { _, isChecked ->
crime = crime.copy(isSolved = isChecked)
}
}
}
}
However, compiling this code gives the error “onCreate overrides nothing”. I can’t find the reason for this problem.
Other Stackoverflow questions indicate that Bundle? causes the problem, because Bundle can’t be null. (even though the Fragment class file in “androidx.fragment.app.Fragment” lists the onCreate method with Bundle? nullable.) My problem is that the code above gives the same compiler error whether “?” is included or note. Something else is apparently wrong.
I would appreciate any tips about the reason.
Case matters. Write onCreate
(with a capital C), not oncreate
.