0

I get the error information

 Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

when I run the code btnOpen.setOnClickListener.

But the system is OK when I run the code

var s= rootView.findViewById<Button>(R.id.btnOpen)

why?

Code

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
    }


}
Sarath Kn
  • 2,680
  • 19
  • 24
HelloCW
  • 843
  • 22
  • 125
  • 310

0 Answers0