This is an error.
| |
ID not found in inflated resource | |
Error | |
Correctness | |
Android | |
Android Open Source Project | |
7.3.0 (September 2022) | |
Kotlin and Java files and resource files | |
This check runs on the fly in the IDE editor | |
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.
You can suppress false positives using one of the following mechanisms:
lint.xml
file in the source tree which turns off
the check in that folder and any sub folder. A simple file might look
like this:
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<issue id="MissingInflatedId" severity="ignore" />
</lint>
Instead of ignore
you can also change the severity here, for
example from error
to warning
. You can find additional
documentation on how to filter issues by path, regular expression and
so on
here.
lintOptions {
disable 'MissingInflatedId'
}
In Android projects this should be nested inside an android { }
block.
lint
, using the --ignore
flag:
$ lint --ignore MissingInflatedId ...`