簡體   English   中英

最終局部變量checkstate不能分配,因為它是用封閉類型定義的

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

我有這樣的課:

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,因此無法分配該變量!

我搜索了google和stackoverflow,但是找不到與我的問題兼容的答案! 你能幫助我嗎? 提前致謝!

您可以創建一個實現OnClickListener的類,甚至您當前的類也可以做到這一點,並以這種方式覆蓋onclick() ,您可以訪問任何變量(不僅是final)

示例: 編輯

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

我不知道您是否打算使用您的代碼。 有點模糊。 正如OnClickListener所說,最好實現OnClickListener接口,以避免將final發送給它的方法。 之后,為什么一切都是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();
                }
             }
            });
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM