Java 设置Android ActionBar颜色时出现“字段可以转换为局部变量”消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31713073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:29:02  来源:igfitidea点击:

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

javaandroidandroid-actionbarandroid-support-libraryandroid-appcompat

提问by MacaronLover

After setting the colour of the Action Bar, actionBarColorin 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?

设置操作栏的颜色后,actionBarColorinprivate String actionBarColor = "#B36305";会突出显示为黄色,并且由于某种原因返回警告。可以做些什么来摆脱这个警告?

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

采纳答案by Mick Mnemonic

What the warning is telling you is that actionBarColorshouldn'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.

警告告诉您它actionBarColor不应该是全局变量(即字段),因为它仅用于一种方法 ( onCreate)。这是一个很好的建议:您应该始终最小化变量的范围,因为它提高了可读性并减少了编程错误的可能性。

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

要消除警告,请通过在 中声明变量来解决问题onCreate

final String actionBarColor = "#B36305";

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

回答by Anurag Mishra

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

当您进入 lint 错误时,这不是一个警告,而不是它会在用作局部变量的类级别变量中显示。去并将其定义为局部变量。它会起作用

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.

我们使用 mtracker 变量作为本地变量,所以我们必须在 oncreate 方法中声明。这将解决您的错误。

Hope this will help you.

希望这会帮助你。

回答by iOSAndroidWindowsMobileAppsDev

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

如果您知道将使用变量,请将其添加到类的顶部:

@SuppressWarnings("FieldCanBeLocal")

@SuppressWarnings("FieldCanBeLocal")