简体   繁体   English

无法解决符号错误

[英]Cannot resolve symbol error

I have been trying and trying to figure this out, but with each change I make, new error comes up. 我一直在尝试并尝试解决这个问题,但是随着我的每次更改,都会出现新的错误。 What I'd like to do is store the values from the numberpickers (I have 2 of them as you can probably see), and then be able to use these values later on. 我想做的是存储数字选择器中的值(您可能会看到其中的2个),然后以后可以使用这些值。 I would like to use them in the Toast message below and in a new activity called countdown once I fix this problem. 修复此问题后,我想在下面的Toast消息中以及在名为countdown的新活动中使用它们。 I get error messages saying that mainTime and snoozeTime are redundant under the MyListener and MyListener2 classes, and "cannot resolve symbol" when I try to use them in my string. 我收到错误消息,说在MyListener和MyListener2类下mainTime和snoozeTime是多余的,当我尝试在字符串中使用它们时,它们“无法解析符号”。

public void openCD(View v) {

    class MyListener implements NumberPicker.OnValueChangeListener {

        @Override
        public void onValueChange(NumberPicker numberPickerMain, int oldVal, int newVal) {
            int mainTime = newVal;
        }
    }

    class MyListener2 implements NumberPicker.OnValueChangeListener {

        @Override
        public void onValueChange(NumberPicker numberPickerSnooze, int oldVal, int newVal) {
            int snoozeTime = newVal;
        }
    }

    String confirmation = "Your shower is set for " + MyListener.mainTime + " minutes with a " 
            + MyListener2.snoozeTime + " minute snooze. Enjoy your shower!";
    Toast.makeText(this.getApplicationContext(), confirmation, Toast.LENGTH_LONG).show();
    Intent countdown=new Intent(this, CountDown.class);
    startActivity(countdown);
}

In the line: 在该行中:

int mainTime = newVal;

you declare the variable locally, which means that it won't be recognized outside of the method. 您可以在本地声明变量,这意味着该变量不会在方法外部被识别。

Same applies to: 同样适用于:

int snoozeTime = newVal;

In order to fix the issue, declare these variables as instance variables (in the class level) and when you assign them, just do the assignment, without a declaration (declaring the type): 为了解决此问题,请将这些变量声明为实例变量(在类级别),并且在分配它们时,只需进行分配,而无需声明(声明类型):

mainTime = newVal;

Try out as below: 尝试如下:

int mainTime=0,snoozeTime=0; 

public void openCD(View v) {

    class MyListener implements NumberPicker.OnValueChangeListener {

        @Override
        public void onValueChange(NumberPicker numberPickerMain, int oldVal, int newVal) {
            mainTime = newVal;
        }
    }

    class MyListener2 implements NumberPicker.OnValueChangeListener {

        @Override
        public void onValueChange(NumberPicker numberPickerSnooze, int oldVal, int newVal) {
            snoozeTime = newVal;
        }
    }

    String confirmation = "Your shower is set for " + mainTime + " minutes with a " 
            + snoozeTime + " minute snooze. Enjoy your shower!";
    Toast.makeText(this.getApplicationContext(), confirmation, Toast.LENGTH_LONG).show();
    Intent countdown=new Intent(this, CountDown.class);
    startActivity(countdown);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM