Can't access EditText or other UI components with Kotlin
Asked Answered
K

5

7

I'm using Android Studio 3.0 RC2 & Kotlin.

When I try to access a UI component the app crashes unless I first write findViewById. I thought Kotlin was supposed to get rid of having to write findViewById line? The UI is a fragment and I'm trying to access from the same fragment code. Is there a way to not have to write findViewById?

These lines work:

var userNameField = view?.findViewById<EditText>(R.id.userNameTextField) as EditText
userNameField.setText("hello world")

This line doesn't work without findViewById line

userNameTextField.setText("hello world")

I even have

import kotlinx.android.synthetic.main.fragment_sign_in.*

The onCreateView() code:

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

    var userNameField = view?.findViewById<EditText>(R.id.userNameTextField) as EditText
    userNameField.setText("hello world")

    return view
}
Karafuto answered 23/10, 2017 at 5:21 Comment(7)
Because it doesn't know that userNameField is an EditText.Anesthesia
Show your onCreate methodOversize
Which kotlin version you are using?Jarry
Check github.com/gradle/kotlin-dsl/issues/377 . It may helps you.Jarry
The view is called userNameTextField but you are using userNameField.Cheree
@Cheree Thanks for the catch, unfortunately this was an error in copying into stackoverflow and not an error in my actual code.Karafuto
@pRaNaY. Version: 1.1.51-release-Studio3.0-1Karafuto
C
3

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")
    }
Cottontail answered 23/10, 2017 at 19:44 Comment(0)
S
3

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'
Seppuku answered 1/11, 2020 at 13:10 Comment(0)
X
2

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.*
Xenia answered 25/11, 2020 at 8:16 Comment(0)
U
0

I faced a similar problem.

I had a private function. I'd also imported the kotlinx library. But I was not able to access the editText from that function.

I just removed the function and defined it again.

Voila! It worked.

Unction answered 10/12, 2018 at 15:7 Comment(0)
S
0

For those who have this problem in 2021:

I know this question is asked in the past but since it is still relevant here I want to help others: today I ran to this problem and tried the suggested ways and unfortunately, the

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

is not working anymore or more accurately is deprecated, so try to use the Jetpack view binding approach and you'll be good.

The message I got after adding the Gradle Kotlin Android extension dependency is: The 'kotlin-android-extensions' Gradle plugin is deprecated. Please use this migration guide (click here) to start working with View Binding. (click here) and the 'kotlin-parcelize' plugin.

Subirrigate answered 11/1, 2021 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.