gpt4 book ai didi

java - 最终局部变量 checkstate 无法分配,因为它是在封闭类型中定义的

转载 作者:行者123 更新时间:2023-12-02 05:06:56 25 4
gpt4 key购买 nike

我有一堂这样的课:

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,但没有找到我的问题的兼容答案!你能帮助我吗?提前致谢!

最佳答案

我不知道你想要处理你的代码。有点模糊。正如 Yazan 所说,最好实现 OnClickListener 接口(interface),以避免将最终结果发送到其方法。那为什么一切都是最终?您必须在类级别定义变量(类变量),以便从其他方法轻松访问它们,并且 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();
}
}
});
}

}

关于java - 最终局部变量 checkstate 无法分配,因为它是在封闭类型中定义的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27704630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com