如何解決 case expressions must be constant expressions
Android Library工程寫程式碼需要注意switch … case語句對資源id的引用,會引發編譯錯誤:case expressions must be constant expressions。
通常android工程生成的資源R.java,資源id宣告如下:
public static final int main=0x7f030004;
而對於library工程,資源id宣告如下:
public static int main=0x7f030004;
缺少了final,因此使用R.id 的switch … case語句會編譯失敗。
解決方法如下:
將switch … case替換成if … else語句。
for Eclipse
Eclipse中,游標定位在switch上,按Ctrl + 1(Command + 1 for Mac osx),選擇Convert ‘switch’ to ‘if-else’.
for Android studio
快捷鍵,Alt + Enter,選擇 Replace ‘switch’ with ‘if’.
如轉換前程式碼:
轉換後程式碼:
相關文章
- Java Lambda ExpressionsJavaExpress
- Expressions v1.3.6Express
- Using Regular Expressions in Oracle DatabaseExpressOracleDatabase
- lambda expressions are not supported at this language level intellijExpressIntelliJ
- Understand Lambda Expressions in 3 minutes(翻譯)Express
- Spring Security 表示式(Expressions) - hasRole示例SpringExpress
- PyCharm使用技巧(六):Regullar Expressions的使用PyCharmExpress
- Regular Expressions in Grep Command with 10 Examples --referenceExpress
- PCRE Perl Compatible Regular Expressions LearningExpress
- POJ-2106 Boolean Expressions-!||&計算BooleanExpress
- jstl c標籤 ”test does not support runtime expressions“JSExpress
- 【譯】節選–揭祕命名函式表示式(Named function expressions )函式FunctionExpress
- 【譯】節選--揭祕命名函式表示式(Named function expressions )函式FunctionExpress
- C#12中的Collection expressions(集合表示式語法糖)C#Express
- 【每日一包0013】to-capital-case,to-constant-case,to-dot-caseAPI
- According to TLD or attribute directive in tag file, attribute value does not accept any expressions報錯解決辦法Express
- SQL SERVER – Fix: Error Msg 128 The name is not permitted in this context. Only constants, expressions, or variables allowed herSQLServerErrorMITContextExpress
- MySQL 8.0 Reference Manual(讀書筆記51節--Optimizing Subqueries, Derived Tables, View References, and Common Table Expressions(1))MySql筆記ViewExpress
- MySQL 8.0 Reference Manual(讀書筆記52節--Optimizing Subqueries, Derived Tables, View References, and Common Table Expressions(2))MySql筆記ViewExpress
- 如何解決Android 5.0中出現的警告:Service Intent must be explicitAndroidIntent
- 使用Git命令時出現fatal: this operation must be run in a work tree提示,該如何解決Git
- 異常解決java.io.IOException: invalid constant type: 15JavaException
- 解決:java.lang.IllegalArgumentException: String must not be emptyJavaException
- It's a must to ghd
- XX must be built with ARCUI
- ImportError: Start directory is not importable: './test_case'怎麼解決?ImportError
- Oracle 條件索引 case when 報錯解決方案Oracle索引
- oracle caseOracle
- oracle plsql case when_end case小記OracleSQL
- sql case語法和plsql case語法!SQL
- find: paths must precede expression:Express
- 解決脫離rails使用activerecord報錯 NameError: uninitialized constant ActiveRecord::Migrator::ZlibAIErrorZed
- oracle的case函式和case控制結構Oracle函式
- ORACLE CASE WHEN 及 SELECT CASE WHEN的用法Oracle
- Flutter:VM snapshot must be valid. Check failed: vm. Must be able to initializeFlutterAI
- Java Case InterviewJavaView
- Java switch caseJava
- case class inheritance