zoukankan      html  css  js  c++  java
  • 在Android library中不能使用switch-case语句访问资源ID的原因分析及解决方案

    转:http://www.jianshu.com/p/89687f618837

    原因分析

      当我们在Android依赖库中使用switch-case语句访问资源ID时会报如下图所示的错误,报的错误是case分支后面跟的参数必须是常数,换句话说出现这个问题的原因是Android library中生成的R.java中的资源ID不是常数:

    打开library中的R.java,发现确实如此,每一个资源ID都没有被声明为final:

    但是当你打开你的主工程,在onClick、onItemClick等各种回调方法中是可以通过switch-case语句来访问资源ID的,因为在主工程的R.java中资源ID都被声明为了final常量。

      project中能够通过switch-case语句正常引用资源ID:

    project中的R.java:

    解决方案

      既然是由于library的R.java中的资源ID不是常量引起的,我们可以在library中通过if-else-if条件语句来引用资源ID,这样就避免了这个错误:

    参考资料:

      为了进一步了解问题的具体原因,在万能的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'.

    问题详见:switch case statement error: case expressions must be constant expression

  • 相关阅读:
    【UWP】仅在TextBlock文本溢出时显示Tooltip
    Vue CSS引用背景图片问题
    使用C#与CefSharp相互调用所踩过的坑
    使用JS在网页上查看显示PDF文件
    VS 代码提示默认不选中 解决办法
    Windows Server 2012 R2更新(KB2919355)
    在ASP.NET Core 3.1中使用Swagger
    SQL SERVER 数据库授权指定用户
    第一篇博客
    观察者模式
  • 原文地址:https://www.cnblogs.com/li-fei/p/4414513.html
Copyright © 2011-2022 走看看