[英]Cannot resolve symbol error
我一直在嘗試並嘗試解決這個問題,但是隨着我的每次更改,都會出現新的錯誤。 我想做的是存儲數字選擇器中的值(您可能會看到其中的2個),然后以后可以使用這些值。 修復此問題后,我想在下面的Toast消息中以及在名為countdown的新活動中使用它們。 我收到錯誤消息,說在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);
}
在該行中:
int mainTime = newVal;
您可以在本地聲明變量,這意味着該變量不會在方法外部被識別。
同樣適用於:
int snoozeTime = newVal;
為了解決此問題,請將這些變量聲明為實例變量(在類級別),並且在分配它們時,只需進行分配,而無需聲明(聲明類型):
mainTime = newVal;
嘗試如下:
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.