我得到了错误信息
Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
当我运行代码btnOpen.setOnClickListener
时。
但是运行代码后系统正常
var s= rootView.findViewById<Button>(R.id.btnOpen)
为什么?
代码
import kotlinx.android.synthetic.main.layout_tab_clipboard.*
import info.dodata.clipboard.R
import utility.openActivity
class UITabClipboard: Fragment(){
private lateinit var mContext: Context
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.layout_tab_clipboard, container, false)
mContext = rootView.context
var s= rootView.findViewById<Button>(R.id.btnOpen) //It's OK
s.setOnClickListener(){
mContext.openActivity<UIAbout>()
}
btnOpen.setOnClickListener { //It cause error
mContext.openActivity<UIAbout>()
}
return rootView
}
}