How to solve error: "Resource IDs cannot be used in switch statement in Android library modules"

Gp Master :

I imported a Android project created by someone else into my project as a library module. I get the following error even after cleaning and rebuilding project:

Constant expression required Resource IDs cannot be used in switch statement in Android library

enter image description here

How do I fix this error?

Michael Dodd :

Your main problem here is that switch statements require constant values as comparators, be it either a literal value e.g. 1, "hello" or a final variable declared at class level. Android R.id values have not been constant since API 14, as stated in that error message, so therefore cannot be used as part of a switch statement.

Your alternative would be to use if else statements as they do not require constant values, like so:

if (v.getId() == R.id.something) {
    // Do something
} else if (v.getId() == R.id.something_else) {
   // Do something else
}
// Repeat however many times required
else {
   // Default value
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=36797&siteId=1