Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 反按时取消倒计时_Android - Fatal编程技术网

Android 反按时取消倒计时

Android 反按时取消倒计时,android,Android,我正试图取消倒计时,我按下计数器。取消;出现“无法解决计数”错误。我想它找不到倒计时! 有一个btn_riazi1_1_1可以取消计时器 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_riazi1_1); //// handling timer

我正试图取消倒计时,我按下计数器。取消;出现“无法解决计数”错误。我想它找不到倒计时! 有一个btn_riazi1_1_1可以取消计时器

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_riazi1_1);


    //// handling timer
    final TextView textic = (TextView) findViewById(R.id.riazi1_1_timer);

    final CountDownTimer Count = new CountDownTimer(5000, 1000) {
        public void onTick(long millisUntilFinished) {

            textic.setText(getResources().getString(R.string.remaintime) + millisUntilFinished / 1000);
        }



        public void onFinish() {
            textic.setText(getResources().getString(R.string.timesup));
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {



                    Intent i=new Intent(Riazi1_1.this,Riazi1_2.class);

                    startActivity(i);
                }
            }, 1000);

        }
    };

    Count.start();
    /////////////////


    //// handling button1
    Button btn1 = (Button) findViewById(R.id.btn_riazi1_1_1);
    btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Count.cancel();

            TextView txtwrong = (TextView) findViewById(R.id.txt_wrong_riazi1_1);
            txtwrong.setVisibility(View.VISIBLE);

            Button btn2 = (Button) findViewById(R.id.btn_riazi1_1_2);
            btn2.setBackgroundColor(Color.GREEN);


            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {



                    Intent i=new Intent(Riazi1_1.this,Riazi1_2.class);

                    startActivity(i);
                }
            }, 1000);



        }
    });

 }

 @Override
public void onBackPressed(){

    super.onBackPressed();
    Count.cancel();     //   error:  Count cannot be resolved ///

}
 }

您已经在onCreate方法中声明了计时器。因此,您无法在onCreate之外访问它。将其声明为成员变量

private CountDownTimer Count; /// declare it as a member variable
在onCreate方法中,将其初始化为

Count = new CountDownTimer(5000, 1000).....

[建议使用命名约定。以大写字母开头的变量命名确实令人困惑]

@farhang67,欢迎您,也欢迎您使用stackoverflow。如果你认为答案有帮助,你可以接受/赞成这个答案