Home » Android » string – Android resource IDs suddenly not final, fields cannot be resolved

string – Android resource IDs suddenly not final, fields cannot be resolved

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m working on a new-old project… I’m making it for a different country market, and when i copyed my code from an old project it shows some mistake like MIGRATE ANDROID CODE (As of ADT 14, resource fields cannot be used as switch cases.) This happens for a resourses that are in strings.xml file and for layout.
But it doesn’t give me some posible fix… how can i fix it???? Here is code:

 @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //Here is mistake
            setContentView(R.layout.main);

          //Kreira AlertDialog sa dva dugmeta koji ce se pojaviti pri aktiviranju aplikacije
            ad = new AlertDialog.Builder(this).create();
//And HERE
            ad.setTitle(getString(R.string.vasa_trenutna_lokacija));
            //And HERE
ad.setMessage(getString(R.string.da_bi_ste_koristili_aplikaciju));
            //And HERE
            ad.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.dozvoli), new DialogInterface.OnClickListener() {

            //Klikom na dugme Dozvoli otvara se novi prozor
            @Override
            public void onClick(DialogInterface ad, int which) {
                Intent i = new Intent(NiskiMerakActivity.this, TrenutnaLokacija.class);             
                startActivity(i);
                finish();
                }
            });

            ad.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.ne_dozvoli),new DialogInterface.OnClickListener() {

            //Klikom na dugme Ne dozvoli aplikacija se zatvara
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();

                }
            });
How to&Answers:

As of ADT 14, resource fields cannot be used as switch cases.

This happened to me too, I got this error even though I wasn’t using the R variable in a switch statement. Don’t be mislead by the error message. You basically have some error that relates to the R.java file. Make sure you don’t import R.java since you should have your own. Delete your R.java file, build project, and see if your R.java file gets generated. If not, you could have an error that is preventing your R.java file from being updated. Try Project > Clean to find any errors. Get rid of the errors and rebuild your project so that your R.java file gets generated and is up-to-date.

Answer:

Quoting http://tools.android.com/tips/non-constant-fields

The solution for this is simple: Convert the switch statement into an if-else statement.

Since your code snippet above does not appear to have a switch() statement, yet your question refers to a switch() statement, I assume you have the wrong code.

Answer:

if your switch-case of the ids is inside a library, sadly the new ADT versions don’t support it.

However, you can convert it easily to if-else , as shown on Google’s website:

http://tools.android.com/tips/non-constant-fields

Answer:

Probably you just need to Clean your project, i.e. Project -> Clean from the main menu.
There should not be errors in the code above. The problem is only with switch statements.
Please try to clean and comment here what is the result.

Answer:

I fixed this by changing my import from android.r; to android.R.*;

It cleared all of my errors and i was able to compile my app again.

Answer:

Today i was facing the same error, my eclipse project have 1 project library added, and i was about to change all the code to if statements, but i realize my project had the library check on, in project ->properties-> android, so i just turn it down and that was the solution to the problem, it also was a suggestion of eclipse itself, reading carefully android migration code adt 14 constants warning for project libraries.

Answer:

I found it works after changing the import statement for R to include your package name.

For instance:

import android.R;

is now

import com.name.package.R;

Answer:

Just select in eclipse project -> clean, and the problem was solved. I got this error just by creating a new package in my app and removing it.

Answer:

Here are the steps which helps me to resolve this problem.

  1. Delete R.java (Eclipse will recreate it immediately. If not there is a problem with the code or layout)
  2. Clean the project (Eclipse menu ‘Project’ & ‘Clean’
  3. Recompile the project

These steps resolve the problem with my work.

Answer:

I tried all the approaches mentioned above regarding deleting the generated R.java file, clean, rebuild, etc but that approach did not work for me.

What did work was replacing the case with an else-if chain which was a messy fix in my opinion but I couldn’t get it to work any other way.

I’m using Eclipse 4.2.1 and Android SDK 4.2.2