Como usar as extensões Kotlin Android com Fragment
s? Se eu usá-los dentro onCreateView()
, recebo esta NullPointerException
exceção:
Causado por: java.lang.NullPointerException: tentativa de chamar o método virtual 'android.view.View android.view.View.ind.findViewById (int)' em uma referência de objeto nulo
Aqui está o código do fragmento:
package com.obaied.testrun.Fragment
import android.os.Bundle
import android.support.v4.app.Fragment
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.obaied.acaan.R
import kotlinx.android.synthetic.main.fragment_card_selector.*
public class CardSelectorFragment : Fragment() {
val TAG = javaClass.canonicalName
companion object {
fun newInstance(): CardSelectorFragment {
return CardSelectorFragment()
}
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
var rootView = inflater?.inflate(R.layout.fragment_card_selector, container, false)
btn_K.setOnClickListener { Log.d(TAG, "onViewCreated(): hello world"); }
return rootView
}
}
`
rootView.btn_K.setOnClickListener