Reason analysis and solutions for accessing resource IDs in the Android library without using the Switch-case statement

Source: Internet
Author: User
Tags case statement switch case

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

Cause analysis

When we use the Switch-case statement to access the resource ID in the Android dependency library, we report the error as shown in the error that the argument followed by the case branch must be a constant, in other words the reason for this problem is that Android The resource ID in the R.java generated in the library is not a constant:

Open the R.java in the library and find that this is true, and each resource ID is not declared final:

But when you open your main project, the resource ID can be accessed through the Switch-case statement in various callback methods such as OnClick, Onitemclick, because the resource ID is declared in the R.java of the main project for the final constant.

The resource ID can be referenced normally through the Switch-case statement in project:

R.java in Project:

Solution Solutions

Since the resource ID in the library's R.java is not constant, we can refer to the resource ID in the library through the IF-ELSE-IF conditional statement, thus avoiding this error:

Resources:

In order to further understand the specific reasons for the problem, in the almighty StackOverflow on the real search for this problem:

In a regular Android project, constants in the resource R class is declared like this:

public static final int main=0x7f030004;

However, as of ADT, in a library project, they'll is declared like this:

public static int main=0x7f030004;

In other words, the constants is not final in a library project. Therefore your code would no longer compile.

The solution for this is simple:convert the switch statement to 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 '.

Questions See: Switch case Statement error:case expressions must be constant expression

Reason analysis and solutions for accessing resource IDs in the Android library without using the Switch-case statement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.