android import library switch语句报错case expressions must be constant expressions
2016-03-29 10:28
666 查看
今天当我从github上下载一个工程,并把它的库文件导入eclipse中,发现switch语句报错case expressions must be constant expressions ;
解决方法是:
先把那个工程的properties->android->is Library勾选上的勾去掉
然后clean一下工程就好了;
因为这个工程我要作为另一个工程的库文件,
所以最后还是要继续勾选上is Library哦!
不过这次不会报错啦!
当然要想根本解决就是把switch-case语句换成if-else
下面是个人感觉解释的比较好的文章
原文链接:http://www.jianshu.com/p/89687f618837
library error
打开library中的R.java,发现确实如此,每一个资源ID都没有被声明为final:
library R.java
但是当你打开你的主工程,在onClick、onItemClick等各种回调方法中是可以通过switch-case语句来访问资源ID的,因为在主工程的R.java中资源ID都被声明为了final常量。
project中能够通过switch-case语句正常引用资源ID:
project right
project中的R.java:
project R.java
library use
In a regular Android project, constants in the resource R class are declared like this:
However, as of ADT 14, in a library project, they will be declared like this:
In other words, the constants are not final in a library project. Therefore your code would no longer compile.
The solution for this is simple: Convert the switch statement into an if-else statement.
http://tools.android.com/tips/non-constant-fields
Tip
You can quickly convert a switch statement to an if-else statement using Eclipse's quick fix.
Click on the switch keyword and press Ctrl + 1 then select
Convert 'switch' to 'if-else'.
解决方法是:
先把那个工程的properties->android->is Library勾选上的勾去掉
然后clean一下工程就好了;
因为这个工程我要作为另一个工程的库文件,
所以最后还是要继续勾选上is Library哦!
不过这次不会报错啦!
当然要想根本解决就是把switch-case语句换成if-else
下面是个人感觉解释的比较好的文章
原文链接:http://www.jianshu.com/p/89687f618837
原因分析
当我们在Android依赖库中使用switch-case语句访问资源ID时会报如下图所示的错误,报的错误是case分支后面跟的参数必须是常数,换句话说出现这个问题的原因是Android library中生成的R.java中的资源ID不是常数:library error
打开library中的R.java,发现确实如此,每一个资源ID都没有被声明为final:
library R.java
但是当你打开你的主工程,在onClick、onItemClick等各种回调方法中是可以通过switch-case语句来访问资源ID的,因为在主工程的R.java中资源ID都被声明为了final常量。
project中能够通过switch-case语句正常引用资源ID:
project right
project中的R.java:
project R.java
解决方案
既然是由于library的R.java中的资源ID不是常量引起的,我们可以在library中通过if-else-if条件语句来引用资源ID,这样就避免了这个错误:library use
参考资料:
为了进一步了解问题的具体原因,在万能的StackOverflow上还真搜到了这个问题:In a regular Android project, constants in the resource R class are declared like this:
public static final int main=0x7f030004;
However, as of ADT 14, in a library project, they will be declared like this:
public static int main=0x7f030004;
In other words, the constants are not final in a library project. Therefore your code would no longer compile.
The solution for this is simple: Convert the switch statement into an if-else statement.
public void onClick(View src) { int id = src.getId(); if (id == R.id.playbtn){ checkwificonnection(); } else if (id == R.id.stopbtn){ Log.d(TAG, "onClick: stopping srvice"); Playbutton.setImageResource(R.drawable.playbtn1); Playbutton.setVisibility(0); //visible Stopbutton.setVisibility(4); //invisible stopService(new Intent(RakistaRadio.this,myservice.class)); clearstatusbar(); timer.cancel(); Title.setText(" "); Artist.setText(" "); } else if (id == R.id.btnmenu){ openOptionsMenu(); } }
http://tools.android.com/tips/non-constant-fields
Tip
You can quickly convert a switch statement to an if-else statement using Eclipse's quick fix.
Click on the switch keyword and press Ctrl + 1 then select
Convert 'switch' to 'if-else'.
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories