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












22















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)));
}
}
}









share|improve this question




















  • 1





    what is the significance of this warning? I mean how problematic can it be from performance or memory use aspect , if left unhandled..?

    – eRaisedToX
    Apr 18 '17 at 6:12
















22















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)));
}
}
}









share|improve this question




















  • 1





    what is the significance of this warning? I mean how problematic can it be from performance or memory use aspect , if left unhandled..?

    – eRaisedToX
    Apr 18 '17 at 6:12














22












22








22


7






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)));
}
}
}









share|improve this question
















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)));
}
}
}






java android android-actionbar android-support-library appcompat






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 22:05







MacaronLover

















asked Jul 29 '15 at 23:51









MacaronLoverMacaronLover

1,97032657




1,97032657








  • 1





    what is the significance of this warning? I mean how problematic can it be from performance or memory use aspect , if left unhandled..?

    – eRaisedToX
    Apr 18 '17 at 6:12














  • 1





    what is the significance of this warning? I mean how problematic can it be from performance or memory use aspect , if left unhandled..?

    – eRaisedToX
    Apr 18 '17 at 6:12








1




1





what is the significance of this warning? I mean how problematic can it be from performance or memory use aspect , if left unhandled..?

– eRaisedToX
Apr 18 '17 at 6:12





what is the significance of this warning? I mean how problematic can it be from performance or memory use aspect , if left unhandled..?

– eRaisedToX
Apr 18 '17 at 6:12












3 Answers
3






active

oldest

votes


















28














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)));
}





share|improve this answer



















  • 1





    How about constants? I like to declare them at the top of my class.

    – Trace
    Nov 11 '18 at 13:08











  • @Trace, yes, you can declare them on the class-level (private|public static final String), and doing so you shouldn't see this warning.

    – Mick Mnemonic
    Nov 11 '18 at 17:20













  • I still get the warning. I'll just ignore it for this use case thanks.

    – Trace
    Nov 11 '18 at 18:14



















36














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



@SuppressWarnings("FieldCanBeLocal")






share|improve this answer



















  • 3





    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

    – yarell
    May 11 '17 at 16:17






  • 2





    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.

    – William
    Feb 16 '18 at 19:27



















-1














This is not a error this is waring when you go in the lint errors than it will show in class level variable which used as a local variable. Go and just define it as a local variable. It will Works



For example -



private Tracker mTracker, mTracker2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GoogleAnalytics mInstance = GoogleAnalytics.getInstance(this);
mTracker = mInstance.getDefaultTracker();
mTracker2 = mInstance.getTracker(URL.ANALYTIC);
mInstance.setDefaultTracker(mTracker2);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.actress_about_detail);
}


we use mtracker variable as a local so we have to declare in oncreate method. This will resolve your error.



Hope this will help you.






share|improve this answer



















  • 1





    But you haven't declared it in onCreate...

    – barry
    Aug 9 '16 at 8:43













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f31713073%2ffield-can-be-converted-to-a-local-variable-message-appearing-when-setting-andr%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









28














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)));
}





share|improve this answer



















  • 1





    How about constants? I like to declare them at the top of my class.

    – Trace
    Nov 11 '18 at 13:08











  • @Trace, yes, you can declare them on the class-level (private|public static final String), and doing so you shouldn't see this warning.

    – Mick Mnemonic
    Nov 11 '18 at 17:20













  • I still get the warning. I'll just ignore it for this use case thanks.

    – Trace
    Nov 11 '18 at 18:14
















28














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)));
}





share|improve this answer



















  • 1





    How about constants? I like to declare them at the top of my class.

    – Trace
    Nov 11 '18 at 13:08











  • @Trace, yes, you can declare them on the class-level (private|public static final String), and doing so you shouldn't see this warning.

    – Mick Mnemonic
    Nov 11 '18 at 17:20













  • I still get the warning. I'll just ignore it for this use case thanks.

    – Trace
    Nov 11 '18 at 18:14














28












28








28







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)));
}





share|improve this answer













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)));
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Jul 30 '15 at 0:02









Mick MnemonicMick Mnemonic

6,47921924




6,47921924








  • 1





    How about constants? I like to declare them at the top of my class.

    – Trace
    Nov 11 '18 at 13:08











  • @Trace, yes, you can declare them on the class-level (private|public static final String), and doing so you shouldn't see this warning.

    – Mick Mnemonic
    Nov 11 '18 at 17:20













  • I still get the warning. I'll just ignore it for this use case thanks.

    – Trace
    Nov 11 '18 at 18:14














  • 1





    How about constants? I like to declare them at the top of my class.

    – Trace
    Nov 11 '18 at 13:08











  • @Trace, yes, you can declare them on the class-level (private|public static final String), and doing so you shouldn't see this warning.

    – Mick Mnemonic
    Nov 11 '18 at 17:20













  • I still get the warning. I'll just ignore it for this use case thanks.

    – Trace
    Nov 11 '18 at 18:14








1




1





How about constants? I like to declare them at the top of my class.

– Trace
Nov 11 '18 at 13:08





How about constants? I like to declare them at the top of my class.

– Trace
Nov 11 '18 at 13:08













@Trace, yes, you can declare them on the class-level (private|public static final String), and doing so you shouldn't see this warning.

– Mick Mnemonic
Nov 11 '18 at 17:20







@Trace, yes, you can declare them on the class-level (private|public static final String), and doing so you shouldn't see this warning.

– Mick Mnemonic
Nov 11 '18 at 17:20















I still get the warning. I'll just ignore it for this use case thanks.

– Trace
Nov 11 '18 at 18:14





I still get the warning. I'll just ignore it for this use case thanks.

– Trace
Nov 11 '18 at 18:14













36














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



@SuppressWarnings("FieldCanBeLocal")






share|improve this answer



















  • 3





    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

    – yarell
    May 11 '17 at 16:17






  • 2





    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.

    – William
    Feb 16 '18 at 19:27
















36














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



@SuppressWarnings("FieldCanBeLocal")






share|improve this answer



















  • 3





    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

    – yarell
    May 11 '17 at 16:17






  • 2





    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.

    – William
    Feb 16 '18 at 19:27














36












36








36







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



@SuppressWarnings("FieldCanBeLocal")






share|improve this answer













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



@SuppressWarnings("FieldCanBeLocal")







share|improve this answer












share|improve this answer



share|improve this answer










answered Sep 26 '16 at 15:16









iOSAndroidWindowsMobileAppsDeviOSAndroidWindowsMobileAppsDev

1,8741726




1,8741726








  • 3





    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

    – yarell
    May 11 '17 at 16:17






  • 2





    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.

    – William
    Feb 16 '18 at 19:27














  • 3





    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

    – yarell
    May 11 '17 at 16:17






  • 2





    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.

    – William
    Feb 16 '18 at 19:27








3




3





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

– yarell
May 11 '17 at 16:17





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

– yarell
May 11 '17 at 16:17




2




2





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.

– William
Feb 16 '18 at 19:27





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.

– William
Feb 16 '18 at 19:27











-1














This is not a error this is waring when you go in the lint errors than it will show in class level variable which used as a local variable. Go and just define it as a local variable. It will Works



For example -



private Tracker mTracker, mTracker2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GoogleAnalytics mInstance = GoogleAnalytics.getInstance(this);
mTracker = mInstance.getDefaultTracker();
mTracker2 = mInstance.getTracker(URL.ANALYTIC);
mInstance.setDefaultTracker(mTracker2);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.actress_about_detail);
}


we use mtracker variable as a local so we have to declare in oncreate method. This will resolve your error.



Hope this will help you.






share|improve this answer



















  • 1





    But you haven't declared it in onCreate...

    – barry
    Aug 9 '16 at 8:43


















-1














This is not a error this is waring when you go in the lint errors than it will show in class level variable which used as a local variable. Go and just define it as a local variable. It will Works



For example -



private Tracker mTracker, mTracker2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GoogleAnalytics mInstance = GoogleAnalytics.getInstance(this);
mTracker = mInstance.getDefaultTracker();
mTracker2 = mInstance.getTracker(URL.ANALYTIC);
mInstance.setDefaultTracker(mTracker2);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.actress_about_detail);
}


we use mtracker variable as a local so we have to declare in oncreate method. This will resolve your error.



Hope this will help you.






share|improve this answer



















  • 1





    But you haven't declared it in onCreate...

    – barry
    Aug 9 '16 at 8:43
















-1












-1








-1







This is not a error this is waring when you go in the lint errors than it will show in class level variable which used as a local variable. Go and just define it as a local variable. It will Works



For example -



private Tracker mTracker, mTracker2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GoogleAnalytics mInstance = GoogleAnalytics.getInstance(this);
mTracker = mInstance.getDefaultTracker();
mTracker2 = mInstance.getTracker(URL.ANALYTIC);
mInstance.setDefaultTracker(mTracker2);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.actress_about_detail);
}


we use mtracker variable as a local so we have to declare in oncreate method. This will resolve your error.



Hope this will help you.






share|improve this answer













This is not a error this is waring when you go in the lint errors than it will show in class level variable which used as a local variable. Go and just define it as a local variable. It will Works



For example -



private Tracker mTracker, mTracker2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GoogleAnalytics mInstance = GoogleAnalytics.getInstance(this);
mTracker = mInstance.getDefaultTracker();
mTracker2 = mInstance.getTracker(URL.ANALYTIC);
mInstance.setDefaultTracker(mTracker2);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.actress_about_detail);
}


we use mtracker variable as a local so we have to declare in oncreate method. This will resolve your error.



Hope this will help you.







share|improve this answer












share|improve this answer



share|improve this answer










answered May 5 '16 at 10:36









Anurag MishraAnurag Mishra

332




332








  • 1





    But you haven't declared it in onCreate...

    – barry
    Aug 9 '16 at 8:43
















  • 1





    But you haven't declared it in onCreate...

    – barry
    Aug 9 '16 at 8:43










1




1





But you haven't declared it in onCreate...

– barry
Aug 9 '16 at 8:43







But you haven't declared it in onCreate...

– barry
Aug 9 '16 at 8:43




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f31713073%2ffield-can-be-converted-to-a-local-variable-message-appearing-when-setting-andr%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine