资源未找到异常:资源 ID#0x7f040015

3

我正在膨胀R.id.itemView以便将其添加到一个gridLayout中:

View item = getLayoutInflater().inflate(R.id.itemView,null);

然而,当我尝试将其填充时,出现了以下错误:
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f040015 type #0x12 is not valid

该资源编号对应于itemView(我在R.java中检查过)。 这是包含itemView的文件(在res/layout中的color_view.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res/com.example.colors"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal">

    <View
        android:id="@+id/itemView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="5dp"
        android:background="@drawable/color_shape" />

</RelativeLayout>

我尝试清理、构建和删除 R.java 文件,但这并不能解决问题。那么这个问题的原因是什么?

2个回答

1
您不能将id提供给inflate的第一个参数,它只能是一个布局。您只能像这样填充布局:
View item = getLayoutInflater().inflate(R.layout.color_view,null);

0

做这个

LayoutInflater inflater = getLayoutInflater();
View layout= inflater.inflate(R.layout.color_view, null);
View viewtoadd = layout.findViewById(R.id.itemView);

你已经完成了使用viewtoadd的操作;干杯!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接