ID not found in inflated resource

ID not found in inflated resource

This is an error.

Id

MissingInflatedId

Summary

ID not found in inflated resource

Severity

Error

Category

Correctness

Platform

Android

Vendor

Android Open Source Project

Feedback

https://issuetracker.google.com/issues/new?component=192708

Since

7.3.0 (September 2022)

Affects

Kotlin and Java files and resource files

Editing

This check runs on the fly in the IDE editor

Implementation

Source Code

Tests

Source Code

Checks calls to layout inflation and makes sure that the referenced ids are found in the corresponding layout (or at least one of them, if the layout has multiple configurations.)

Example

Here is an example of lint warnings produced by this check:

src/test/pkg/MyActivity.kt:15:Error: @layout/activity_main does not
contain a declaration with id text_field [MissingInflatedId]
    requireViewById<EditText>(R.id.text_field).isEnabled = false
                              ---------------
src/test/pkg/MyActivity.kt:21:Error: @layout/list_item does not contain
a declaration with id image_view [MissingInflatedId]
    val imgView = rootView.findViewById<ImageView>(R.id.image_view)
                                                   ---------------

Here are the relevant source files:

src/test/pkg/MyActivity.kt:

package test.pkg import android.app.Activity import android.graphics.ColorFilter import android.os.Bundle import android.view.View import android.widget.EditText import android.widget.ImageView class MyActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Error: @id/text_field is not in @layout/activity_main requireViewById<edittext>(R.id.text_field).isEnabled = false } fun createListItem(filter: ColorFilter): View { val rootView = layoutInflater.inflate(R.layout.list_item, null) // Error: @id/image_view is not in @layout/list_item val imgView = rootView.findViewById<imageview>(R.id.image_view) imgView.colorFilter = filter return rootView } }

res/layout/activity_main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" />

res/layout/list_item.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" />

test.pkg:

@id/text_field

You can also visit the source code for the unit tests for this check to see additional scenarios.

Suppressing

You can suppress false positives using one of the following mechanisms:

formatted by Markdeep 1.18