NoSuchFieldError: No static field listView1 of type I in class Lcom/disdemo/R$id; or its superclasses
AndroidAndroid Problem Overview
I have module1 (in Android Studio) with listView1 in activity_main.xml
(present in res of the module1). The MainActivity of this module is being launched from another module2 in the same Android Studio project.
I have tried deleting the module1 and again creating a new one with same res and java files. Still I am getting the same issue.
Android Solutions
Solution 1 - Android
I think you have layout in both library and module with same name or inflating multiple xml layout with duplicate resource id.
Solution 2 - Android
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.
Solution 3 - Android
you have use multiple module and give same name of any xml layout then give this error so please rename your xml layout.
Solution 4 - Android
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
Solution 5 - Android
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 https://stackoverflow.com/questions/32317822/rebuild-required-after-changing-xml-layout-files-in-android-studio for details.
Solution 6 - Android
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.
Solution 7 - Android
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>;
}
Solution 8 - Android
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.
Solution 9 - Android
I had this issue when using viewBinding
. Class name was SearchViewHolder
and layout resourse name was search_view_holder
. That caused confusion and crash.