NullPointerException al acceder a View en fragmento Kotlin: causas y soluciones
¡Hola a todos! Soy nuevo en Kotlin y desarrollo Android. Me encontré con un problema: al intentar acceder a elementos de la interfaz (TextView, Button) dentro de un fragmento, obtengo NullPointerException. Aquí está mi código:
```kotlin
class MyFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_my, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Aquí intento acceder a la view
val textView = view.findViewById(R.id.my_text_view)
textView.text = "Hola" // NPE!
}
}
```
Pero a veces uso kotlin-android-extensions