Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.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/3/android/206.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
Java 计时器代码使我的应用程序崩溃_Java_Android_Timer_Android Studio_Onstart - Fatal编程技术网

Java 计时器代码使我的应用程序崩溃

Java 计时器代码使我的应用程序崩溃,java,android,timer,android-studio,onstart,Java,Android,Timer,Android Studio,Onstart,我正在学习android开发,我正在尝试做的是有一个从40分钟开始倒计时的标签,当它达到0时,它将停止计数并做其他事情。这是我的代码: @Override protected void onStart() { super.onStart(); count = 2400; final Timer t = new Timer();//Create the object t.scheduleAtFixedRate(new TimerTas

我正在学习android开发,我正在尝试做的是有一个从40分钟开始倒计时的标签,当它达到0时,它将停止计数并做其他事情。这是我的代码:

@Override
protected void onStart() {
        super.onStart();
        count = 2400;
        final Timer t = new Timer();//Create the object
        t.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                minLeft = (int) Math.floor(count / 60);
                secLeft = count - minLeft * 60;
                counter = minLeft + ":" + secLeft;
                TextView tv = (TextView) findViewById(R.id.timer);

                Log.i(MainActivity.TAG,minLeft+", "+secLeft+", "+counter);
                tv.setText(counter);
                count--;
                if (minLeft <= 0 && secLeft <= 0) {
                    t.cancel();
                    count = 2400;
                    onFinish();
                }
            }
        }, 1000, 1000);
    }
@覆盖
受保护的void onStart(){
super.onStart();
计数=2400;
final Timer t=new Timer();//创建对象
t、 scheduleAtFixedRate(新TimerTask(){
@凌驾
公开募捐{
minLeft=(int)数学楼层(计数/60);
secLeft=count-minLeft*60;
计数器=minLeft+“:”+secLeft;
TextView tv=(TextView)findViewById(R.id.timer);
Log.i(MainActivity.TAG,minLeft+”,“+secLeft+”,“+counter);
tv.setText(柜台);
计数--;

如果(minLeft您的计划任务在后台线程上运行。 然后尝试将文本设置为来自此背景线程的textview。 然而,在Android中,所有与视图相关的操作都必须在主线程上完成

这就是在计划任务中,您必须使用以下内容:

@Override
protected void onStart() {
    super.onStart();
    count = 2400;
    final Timer t = new Timer();//Create the object
    t.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            minLeft = (int) Math.floor(count / 60);
            secLeft = count - minLeft * 60;
            counter = minLeft + ":" + secLeft;
            // the name of your actual activity
            MyActivity.this.runOnUiThread(new Runnable() {
               @Override
               public void run() {
                 TextView tv = (TextView) findViewById(R.id.timer);
                 Log.i(MainActivity.TAG,minLeft+", "+secLeft+", "+counter);
                 tv.setText(counter);
               }
            });

            count--;
            if (minLeft <= 0 && secLeft <= 0) {
                t.cancel();
                count = 2400;
                onFinish();
            }
        }
    }, 1000, 1000);
}
@覆盖
受保护的void onStart(){
super.onStart();
计数=2400;
final Timer t=new Timer();//创建对象
t、 scheduleAtFixedRate(新TimerTask(){
@凌驾
公开募捐{
minLeft=(int)数学楼层(计数/60);
secLeft=count-minLeft*60;
计数器=minLeft+“:”+secLeft;
//您实际活动的名称
MyActivity.this.rununuithread(新的Runnable(){
@凌驾
公开募捐{
TextView tv=(TextView)findViewById(R.id.timer);
Log.i(MainActivity.TAG,minLeft+”,“+secLeft+”,“+counter);
tv.setText(柜台);
}
});
计数--;
如果(左)