NoSuchFieldError: 类 Lcom/disdemo/R $id 中没有 I 类型的静态字段 listView1; 或者它的超类

我有模块1(在 Android Studio 中)和 activity_main.xml中的 listView1(存在于模块1的分辨率中)。这个模块的 MainActivity 是从同一个 Android Studio 项目中的另一个模块2启动的。

我已经尝试删除模块1,并再次创建一个新的具有相同的 res 和 java 文件。我还是有同样的问题。

45829 次浏览

I think you have layout in both library and module with same name or inflating multiple xml layout with duplicate resource id.

you have use multiple module and give same name of any xml layout then give this error so please rename your xml layout.

In both modules, you have an activity_main.xml. Please rename one file.

Update: What @kundan kamal wants to express is, you have a layout file in both modules with a similar name.

In my case identifiers were different. I pressed Build > Rebuild Project. This is because I divided res/layout into subfolders, and AS stopped to notice XML changes. See Rebuild required after changing xml layout files in Android Studio for details.

This kind of error happened once to me, but with a string resource. I had the string configured in a translation file, but not in the default string resource file, so this produced the crash.

Make me correct If I am not wrong... I think you have the same name of your library and module in your project so you have to rename one of them then its work fine

For me proguard rules worked. I had two level proguard. First in my dependency and then in my app. Had to do this in my dependency gradle.

Preserve R.. things.

-keepclassmembers class **.R$* {
public static <fields>;
}

Preserve all native method names and the names of their classes.

-keepclasseswithmembernames class * {
native <methods>;
}

I had this issue when using viewBinding. Class name was SearchViewHolder and layout resourse name was search_view_holder. That caused confusion and crash.

Check if your xml is in same module/package/project where you have written the logic to show the code. In my case my xml was in different module which was not accessible.