2014-09-03 142 views
0

我一直在试图弄清楚这一点,但随着每次更改,新错误都会出现。我想要做的是存储数字选择器中的值(我可以看到其中的2个),然后可以稍后使用这些值。我想在下面的Toast消息中使用它们,以及在解决此问题后,在一个名为倒计时的新活动中使用它们。我收到错误消息,指出在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); 
} 
+0

声明你的'mainTime,snoozeTime'水珠盟友在方法之外访问其值。 – GrIsHu 2014-09-03 05:20:12

回答

0

试试如下:

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

这和我的答案有所不同......什么? – alfasin 2014-09-03 07:21:37

+0

这工作就像一个魅力!非常感谢!!! – 2014-11-05 05:24:14

+0

欢迎您将标记为正确的答案,如果有帮助:) @RebeccaWyler – GrIsHu 2014-11-05 05:26:51

0

在行:

int mainTime = newVal; 

你声明变量局部的,这意味着它不会方法的外部识别。

同样适用于:

int snoozeTime = newVal; 

为了解决这个问题,声明这些变量为实例变量(在类级别),当你给它们,只是做任务,没有一个声明(声明类型):

mainTime = newVal; 
相关问题