简体   繁体   中英

The final local variable checkstate cannot be assigned, since it is defined in an enclosing type

I have a class like this:

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

}

but I get this error on the timerStatue method for checkstate = false and checkstate = true: The final local variable checkstate cannot be assigned, since it is defined in an enclosing type!

I searched google and stackoverflow but did not find compatible answer for my problem! Can you help me? thanks in advance!

you can make a class that implements OnClickListener or even your current class can do that, and override onclick() in that way you can access any variable (not only final)

example: Edit

1) make you activity implements OnClickListener

2) override the method onClick() in your activity.

3) move boolean checkstate =false; at class level (outside of onCreate() )

4) change startTimer3Button.setOnClickListener(... to startTimer3Button.setOnClickListener(TimerActivity.this);

6) move the code inside onClick() that exist in timerStatus() to the onclick() added to the activity class

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

I don't know want you meant to do with your code. It's a little fuzzy. As the Yazan said it's better to implement OnClickListener interface to avoid sending final to it's method. After that why everything is final ? You must define your variables in class level (class variables) to access them from other methods easily and the onCreate() method set their primary values. So whatever I fixed your code like below without any error but I still didn't what It meant to do :

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM