Issue
I have a layout xml where the following line occurs:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/rootLayout"
...
and in Kotline I have the following line to refer to this element:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rootLayout = findViewById(R.id.rootLayout)
...
The findViewById function is underlined in red telling 'Not enough information to infer type variable T'
Why is this happening. Clearly the type should be 'ConstraintLayout'
Why the error?
Solution
You must be using API level 26 (or above)
. This version has changed the signature of View.findViewById()
- see here : https://developer.android.com/about/versions/oreo/android-8.0-changes#fvbi-signature
So in your case, where the result of findViewById
is ambiguous, you need to supply the type:
1 Change :
val rootLayout = findViewById(R.id.rootLayout)
val rootLayout = findViewById<ConstraintLayout>(R.id.rootLayout)
2 Change :
this.rootLayout = row?.findViewById(R.id.rootLayout)
this.rootLayout = row?.findViewById<ConstraintLayout>(R.id.rootLayout)
Note that in 2. the cast is only required because the row
is nullable. If the label
was nullable too, or if you made the row not nullable, it wouldn't be required.
Answered By - BADSHAH
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.