无法在Android中正确使用处理程序

时间:2016-06-04 05:48:02

标签: java android

我正在学习android开发,现在我正在制作一个简单的计时器,我需要一个处理程序来保持它的运行。如果我将处理程序放在主要活动中,所有商品,但我想把方法放在主要活动之外,这样我可以重用代码,如果我想改变任何东西,应该更灵活。

这是我的代码:

*public class Timer{

    public void runTimer(TextView view, int seconds, boolean running){
        TextView timerTextView = (TextView) view;
        Handler handler = new Handler();
        MyRunnable r = new MyRunnable(handler,view,seconds,running);
        handler.post(r);

    }



}

class MyRunnable implements Runnable {
    private TextView view;
    private int seconds;
    private boolean running;
    private Handler handler;

    public MyRunnable(Handler handler, TextView view, int seconds, boolean running){
        this.handler= handler;
        this.view =view;
        this.seconds = seconds;
        this.running = running;
    }

    public void run(){
        TextView timerTextView = (TextView) view;
        int hours = seconds/3600;
        int minutes = (seconds%3600)/60;
        int secs = seconds%60;
        String time = String.format("%d:%02d:%02d",hours,minutes,secs);
        timerTextView.setText(time);
        if (running){
            seconds++;
        }
        handler.postDelayed(this,100);
    }

}*

我在主活动中创建了一个对象来调用此函数。该应用程序可以运行,但计时器无法正常工作。它只运行第一次,然后停止。它不会继续运行。你知道问题出在哪里吗?谢谢你的帮助!

主要代码:

*package com.mad.chapter4;

import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class StopWatchActivity extends AppCompatActivity {

    private Timer timer = new Timer();


    private int seconds = 0;
    private boolean running = false;
    private boolean wasRunning;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stop_watch);
        TextView ttv = (TextView) findViewById(R.id.timerTextView);

            if(savedInstanceState!=null){
                seconds = savedInstanceState.getInt("seconds");
                running = savedInstanceState.getBoolean("running");
                wasRunning = savedInstanceState.getBoolean("wasRunning");
            }

        seconds = timer.runTimer(ttv,seconds,running);
    }

    public void onSaveInstanceState (Bundle savedInstanceState){
        savedInstanceState.putInt("seconds",seconds);
        savedInstanceState.putBoolean("running",running);
        savedInstanceState.putBoolean("wasRunning",wasRunning);
    }

    protected void onStop(){
        super.onStop();
        wasRunning = running;
        running = false;
    }

    protected void onStart(){
        super.onStart();
        if(wasRunning){
            running = true;
        }
    }



    public void onClickStartButton(View view){
        running = true;
    }

    public void onClickStopButton (View view){
        running = false;
    }

    public void onClickResetButton (View view){
        seconds = 0;
        running = false;
    }



//    public void runTimer(){
//
//        final TextView timerTextView = (TextView) findViewById(R.id.timerTextView);
//        final Handler handler = new Handler();
//
//        Runnable r = new Runnable(){
//            @Override
//            public void run(){
//                int hours = seconds/3600;
//                int minutes = (seconds%3600)/60;
//                int secs = seconds%60;
//                String time = String.format("%d:%02d:%02d",hours,minutes,secs);
//                timerTextView.setText(time);
//                if (running){
//                    seconds++;
//                }
//                handler.postDelayed(this,100);
//            }
//        };
//
//        handler.post(r);
//
//
//    }


}*

1 个答案:

答案 0 :(得分:1)

从我看到的方式来看,您的Scanner sc = new Scanner(new File("test.asm")); Pattern p = Pattern.compile("^([a-z0-9_-]*)\\s+([A-Z]+)\\s+(.*)"); Matcher m = p.matcher(""); while (sc.hasNextLine()) { String lineFromFile = sc.nextLine(); if (m.reset(lineFromFile).find()) { System.out.printf("%-8s %-6s %s%n", m.group(1), m.group(2), m.group(3)); } } running永远不会设置为true,而这反过来又不是第一次更新您的UI,而是在此之后不再更新。在onCreate中调用wasRunning之前,请尝试将runningwasRunning设置为true。我认为当您在活动中调用此代码时,您正在调用timer.runTimer(ttv, seconds, running),这会将onClickStartButton(View view)标记设置为true并正确更新您的UI。

此外,您的running函数不返回任何内容,只是您在代码中将此函数结果赋给int。这是复制/粘贴错误吗?