"Field can be converted to a local variable" message appearing when setting Android ActionBar colour

47,554

Solution 1

What the warning is telling you is that actionBarColor shouldn't be a global variable (i.e. a field), because it's only used in one method (onCreate). This is good advice: you should always minimize the scope of your variables, because it improves readability and reduces possibilities for programming errors.

To get rid of the warning, fix the problem by declaring the variable within onCreate:

final String actionBarColor = "#B36305";

if(actionBar != null) {
    actionBar.setBackgroundDrawable(
        new ColorDrawable(Color.parseColor(actionBarColor)));
}

Solution 2

If you know you will use the variable(s), add to the top of your class:

@SuppressWarnings("FieldCanBeLocal")

Share:
47,554
wbk727
Author by

wbk727

Apparently, this user prefers to keep an air of mystery about them.

Updated on July 09, 2022

Comments

  • wbk727
    wbk727 almost 2 years

    After setting the colour of the Action Bar, actionBarColor in private String actionBarColor = "#B36305"; gets highlighted yellow and a warning is returned for some reason. What can be done to get rid of this warning?

    Field can be converted to a local variable

    public class MainActivity extends AppCompatActivity {
    
        private String actionBarColor = "#B36305";
    
        private int getFactorColor(int color, float factor) {
            float[] hsv = new float[3];
            Color.colorToHSV(color, hsv);
            hsv[2] *= factor;
            color = Color.HSVToColor(hsv);
            return color;
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.fragment_activity_main);
    
            ActionBar actionBar = getSupportActionBar();
            if(actionBar != null) {
                actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor(actionBarColor)));
            }
        }
    }
    
  • yarell
    yarell about 7 years
    this is what i needed. I have a field. it could have been converted to a local variable, but it was used inside of a loop and i didn't want to re-initialize it over and over every time the loop executed
  • William
    William about 6 years
    Just a note, this is often needed if your field is added to an object that manages its containees using weak references. If you convert to a local variable it will be garbage collected.
  • html_programmer
    html_programmer over 5 years
    How about constants? I like to declare them at the top of my class.
  • Mick Mnemonic
    Mick Mnemonic over 5 years
    @Trace, yes, you can declare them on the class-level (private|public static final String), and doing so you shouldn't see this warning.
  • html_programmer
    html_programmer over 5 years
    I still get the warning. I'll just ignore it for this use case thanks.
  • Drakes
    Drakes about 5 years
    This is the correct answer. Readability is better when magic numbers and strings are declared at the top of the class, and private static makes them constants which is even clearer.
  • Mr Heelis
    Mr Heelis almost 5 years
    THANK YOU !!! (so much!) in mine I needed it later in some self reflection
  • Fran Marzoa
    Fran Marzoa almost 5 years
    I came for that explicitly!
  • Thomas
    Thomas over 3 years
    @html_programmer I got the warning because I forgot the static. Adding it removed the warning.