Android Studio告诉我使用区域设置变量

时间:2017-04-29 13:10:41

标签: android textview

在我的Activity中,我在onCreate方法中创建了许多TextView。所有TextView都声明为私有类变量。但我不是仅在onCreate方法中使用这些TextViews而是在另一种方法中使用。所以我不明白这个警告。

public class ActAchievement extends AppCompatActivity {

    private TextView tvAchieve1;
    private TextView tvAchieve2;
    private TextView tvAchieve3;
    private TextView tvAchieve4;
    private TextView tvAchieve5;
    private TextView tvAchieve6;
    private TextView tvAchieveSub2;
    private TextView tvAchieveSub3;
    private TextView tvAchieveSub4;
    private TextView tvAchieveSub5;
    private TextView tvAchieveSub6;
    private TextView tvCurrentRank;
    private TextView tvCurrentQuestions;
    private int correctQuestions;
    private Cursor questionCursor;
    private String currentRank;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_achievement);

        tvCurrentRank = (TextView) findViewById(R.id.tv_current_rank);
        tvCurrentQuestions = (TextView) findViewById(R.id.tv_current_questions);
        tvAchieve1 = (TextView) findViewById(R.id.tv_achieve1);
        tvAchieve2 = (TextView) findViewById(R.id.tv_achieve2);
        tvAchieve3 = (TextView) findViewById(R.id.tv_achieve3);
        tvAchieve4 = (TextView) findViewById(R.id.tv_achieve4);
        tvAchieve5 = (TextView) findViewById(R.id.tv_achieve5);
        tvAchieve6 = (TextView) findViewById(R.id.tv_achieve6);
        tvAchieveSub2 = (TextView) findViewById(R.id.tv_achieve_sub2);
        tvAchieveSub3 = (TextView) findViewById(R.id.tv_achieve_sub3);
        tvAchieveSub4 = (TextView) findViewById(R.id.tv_achieve_sub4);
        tvAchieveSub5 = (TextView) findViewById(R.id.tv_achieve_sub5);
        tvAchieveSub6 = (TextView) findViewById(R.id.tv_achieve_sub6);

        currentRank = getResources().getString(R.string.currentRank);

        detectRank();
    }

    public void detectRank() {
        questionCursor = MyDatabase.getInstance(this).getCorrectQuestions();
        correctQuestions = questionCursor.getInt(questionCursor.getColumnIndexOrThrow((MyDatabase.ResultColumns.CORRECT)));

        tvCurrentRank.setText(currentRank + " " + tvAchieve1.getText());
        tvCurrentQuestions.setText(correctQuestions + "/255");

        if (correctQuestions >= 5) {
            setRank(tvAchieve2, tvAchieveSub2);
        }

        if (correctQuestions >= 10) {
            setRank(tvAchieve3, tvAchieveSub3);
        }

        if (correctQuestions >= 150) {
            setRank(tvAchieve4, tvAchieveSub4);
        }

        if (correctQuestions >= 200) {
            setRank(tvAchieve5, tvAchieveSub5);
        }

        if (correctQuestions == 255) {
            setRank(tvAchieve6, tvAchieveSub6);
        }
    }

    public void setRank(TextView tvAchieve, TextView tvAchieveSub) {
        tvCurrentRank.setText(currentRank + " " + tvAchieve.getText());
        tvAchieve.setTextColor(ContextCompat.getColor(this, R.color.colorAccent));
        tvAchieveSub.setTextColor(ContextCompat.getColor(this, R.color.colorAccent));
    }
}

如果我在onCreate方法中将变量更改为成员变量,那么我必须使用所有TextViews作为参数调用第二个方法。

您是否了解Android Studio对消息的含义?

1 个答案:

答案 0 :(得分:1)

我相信这些警告有可能是错误的。把它作为一个建议。您必须手动操作。你可以接受它,或者如果它不适合你的情况就压制它。