- 使用'kotlin-android-extensions'可以直接引用xml的资源id使用,不用再find。但要注意,通过inflate得到的(没有setContentView)会遇到view为空的报错,比如在Fragment里,就不能直接引用id,得用find
class NoteFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater!!.inflate(R.layout.fragment_note, container, false)
//tv_hello.text = "hello world" //这句,会报tv_hello view为空的崩溃
view.find<TextView>(R.id.tv_hello).text = "hello world"
return view
}
}