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

时间:2014-12-30 11:28:58

标签: 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();
            }
         }
        });
}

}

但是我在timerStatue方法上遇到checkstate = false和checkstate = true的错误:  无法分配最终的局部变量checkstate,因为它是在封闭类型中定义的!

我搜索了谷歌和stackoverflow但没有找到我的问题的兼容答案! 你能帮助我吗? 提前谢谢!

2 个答案:

答案 0 :(得分:1)

你可以创建一个实现OnClickListener的类,甚至你当前的类都可以这样做,并以这种方式覆盖onclick()你可以访问任何变量(不仅是最终的)

示例:修改

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();
        }
    }
}

答案 1 :(得分:1)

我不知道您是否想要使用您的代码。这有点模糊。正如Yazan所说,最好实现OnClickListener接口以避免向其发送最终方法。 那之后为什么一切都是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();
                }
             }
            });
    }

}