![](/img/trans.png)
[英]Java: the final local variable cannot be assigned, since it was defined in an enclosing type
[英]The final local variable checkstate cannot be assigned, since it is defined in an enclosing type
我有这样的课:
public class TimerActivity extends Activity
{
CountDownTimer cntTimer = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timers);
final ImageButton startTimerButton = (ImageButton)findViewById(R.id.timer1ImageButton);
final EditText timerText = (EditText) findViewById(R.id.timerEditText1);
final TextView timerTextValue = (TextView) findViewById(R.id.timerTextView);
boolean checkstate =false;
timerCountDown(checkstate,startTimer3Button,timerText3, timerTextValue3);
}
public void timerCountDown(boolean check,final ImageButton startTimerImageButton ,
final EditText timerText,final TextView timerTextValue)
{
Integer input = 0;
if(timerText.getText().toString()!="")
{
input = Integer.parseInt(timerText.getText().toString())*1000 ;
}
CountDownTimer timer = new CountDownTimer(input, 1000)
{
public void onTick(long millisUntilFinished)
{
timerTextValue.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish()
{
timerTextValue.setText("done!");
}
};
timerStatus(check,startTimerImageButton,timer);
}
public void timerStatus(final boolean checkstate, final ImageButton startTimer3Button ,final CountDownTimer downTimer)
{
startTimer3Button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
if(checkstate==false)
{
startTimer3Button.setImageResource(R.drawable.reset);
//Error
checkstate = true;
downTimer.start();
}
else
{
startTimer3Button.setImageResource(R.drawable.start);
//Error
checkstate = false;
downTimer.cancel();
}
}
});
}
}
但是我在timerStatue方法的checkstate = false和checkstate = true上遇到此错误:由于已在封闭类型中定义了最终的局部变量checkstate,因此无法分配该变量!
我搜索了google和stackoverflow,但是找不到与我的问题兼容的答案! 你能帮助我吗? 提前致谢!
您可以创建一个实现OnClickListener
的类,甚至您当前的类也可以做到这一点,并以这种方式覆盖onclick()
,您可以访问任何变量(不仅是final)
示例: 编辑
1)让你的活动实现OnClickListener
2)在您的活动中覆盖方法onClick()。
3)移动boolean checkstate =false;
在类级别( onCreate()
)
4)将startTimer3Button.setOnClickListener(...
)更改为startTimer3Button.setOnClickListener(TimerActivity.this);
6)将timerStatus()中存在的onClick()内部的代码移动到添加到活动类的onclick()中
public class TimerActivity extends Activity implements OnClickListener{
//code and methods ....
public void timerStatus(final boolean checkstate, final ImageButton startTimer3Button ,final CountDownTimer downTimer)
{
startTimer3Button.setOnClickListener(TimerActivity.this);
}
//code and methods ....
@override
public void onClick(View v){
if(checkstate==false)
{
startTimer3Button.setImageResource(R.drawable.reset);
//Error
checkstate = true;
downTimer.start();
}
else
{
startTimer3Button.setImageResource(R.drawable.start);
//Error
checkstate = false;
downTimer.cancel();
}
}
}
我不知道您是否打算使用您的代码。 有点模糊。 正如OnClickListener
所说,最好实现OnClickListener
接口,以避免将final发送给它的方法。 之后,为什么一切都是final
? 您必须在类级别定义变量(类变量)才能从其他方法轻松访问它们,并且onCreate()
方法设置其主要值。 因此,无论我如何像下面那样修复您的代码,都没有任何错误,但是我仍然没有做到这一点:
public class TimerActivity extends Activity
{
CountDownTimer cntTimer = null;
ImageButton startTimerButton;
EditText timerText;
TextView timerTextValue;
boolean checkstate =false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timers);
startTimerButton = (ImageButton)findViewById(R.id.timer1ImageButton);
timerText = (EditText) findViewById(R.id.timerEditText1);
timerTextValue = (TextView) findViewById(R.id.timerTextView);
timerCountDown();
}
public void timerCountDown()
{
Integer input = 0;
if(timerText.getText().toString()!="")
{
input = Integer.parseInt(timerText.getText().toString())*1000 ;
}
CountDownTimer timer = new CountDownTimer(input, 1000)
{
public void onTick(long millisUntilFinished)
{
timerTextValue.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish()
{
timerTextValue.setText("done!");
}
};
timerStatus(timer);
}
public void timerStatus(final CountDownTimer downTimer)
{
startTimerButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
if(checkstate==false)
{
startTimerButton.setImageResource(android.R.drawable.ic_secure);
//Error
checkstate = true;
downTimer.start();
}
else
{
startTimerButton.setImageResource(android.R.drawable.ic_delete);
//Error
checkstate = false;
downTimer.cancel();
}
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.