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

标签 java android

我有一堂这样的课:

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/

相关文章:

java - NoSuchMethodException : springframework. boot.autoconfigure.http.HttpMessageConverters

java - 变量声明 - 尽可能使用修饰符 'final'。是的,类和方法怎么了?

android - 如何仅在返回堆栈中保留第一个添加的 fragment ( fragment 重叠)?

java - Str.lastIndexOf ("\")给出错误

java - 在本地存储库中安装第三方jar是否需要pom文件配置?

android - 'texelFetch' : no matching overloaded function found in opengl es 3. 0

android - 当包含在 androidTestCompile 中时,Espresso 将找不到 View

android - 在哪里放置不同大小的图标?

java - 过滤自定义数据结构的设计模式

android - 所有 Android 设备都有 "option"和 "back"按钮吗?