【发布时间】:2015-02-26 13:40:21
【问题描述】:
我有这样的课:
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();
}
}
});
}
}
但我在 checkstate = false 和 checkstate = true 的 timerStatue 方法上收到此错误: 最终的局部变量 checkstate 不能赋值,因为它是在封闭类型中定义的!
我搜索了 google 和 stackoverflow,但没有为我的问题找到兼容的答案! 你能帮助我吗? 提前致谢!
【问题讨论】:
-
没有一个相关答案(列在右侧)让您任何了解问题?
-
您确定这是您的最终代码版本吗?特别是
timerStatus(boolean checkstate部分,因为我认为它应该是timerStatus(final boolean checkstate这就是导致错误的原因,你不能重新分配最终变量。 -
@WilliamPrice 我关注了这个问题,但没有解决我的问题。 :(
-
@Yazan 我修复了这个错误!但是 checkstate = false 和 checkstate =true 给我同样的错误。
-
@Yazan 如果我不使用 final 我无法访问 onClick 方法,如果我使用 final 我无法重新分配它!所以我该怎么做? :(