Can't access EditText or other UI components with Kotlin

In the onCreateView just return the inflated view.

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    // Inflate the layout for this fragment
return inflater!!.inflate(R.layout.fragment_sign_in, container, false)

}

In the onViewCreated you can access your view components

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        userNameField.setText("hello world")
    }

I was brought here by searching for the same issue. I missed to add the kotlin-android-extensions to the build.gradle:

apply plugin: 'kotlin-android-extensions'

Was unable to access the ui components like OP faced looks like below was needed inside app gradle:

plugins {
    ...
    id 'kotlin-android-extensions'
}

Even though after adding this line android studio was still not able to auto resolve the imports for kotlin synthetics so you may need to invalidate cache and restart.

If still not working then import manually depending on views

  • activity /fragment view: import kotlinx.android.synthetic.main.<your_activity_view>.*
  • normal views : import kotlinx.android.synthetic.main.<your_layout_view>.view.*