"Field can be converted to a local variable" message appearing when setting Android ActionBar colour
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")
wbk727
Apparently, this user prefers to keep an air of mystery about them.
Updated on July 09, 2022Comments
-
wbk727 almost 2 years
After setting the colour of the Action Bar,
actionBarColor
inprivate 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 about 7 yearsthis 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 about 6 yearsJust 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 over 5 yearsHow about constants? I like to declare them at the top of my class.
-
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 over 5 yearsI still get the warning. I'll just ignore it for this use case thanks.
-
Drakes about 5 yearsThis 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 almost 5 yearsTHANK YOU !!! (so much!) in mine I needed it later in some self reflection
-
Fran Marzoa almost 5 yearsI came for that explicitly!
-
Thomas over 3 years@html_programmer I got the warning because I forgot the
static
. Adding it removed the warning.