简单的计时器应用程序在android中不起作用

时间:2017-02-21 18:30:05

标签: android

我正在制作一个简单的秒表应用程序。但是在手机上打开时会崩溃。当单独作为一个简单的Java应用程序运行时,Java代码工作正常。但是,在使用startpausereset按钮在Android中实现此java代码时,它无法正常工作..

这是代码......

public class MainActivity extends AppCompatActivity {

private boolean running;
private int second;
private TextView display;

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

    running = false;
    second = 0;

    Button start_button = (Button)findViewById(R.id.start);
    Button pause_button = (Button)findViewById(R.id.pause);
    Button reset_button = (Button)findViewById(R.id.reset);
    display = (TextView)findViewById(R.id.textView2);

    start_button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            running = true;
            startTimer();
        }
    });

    pause_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            running = false;
            startTimer();
        }
    });

    reset_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            running = false;
            second = 0;
            display.setText("");
            display.setHint("00:00:00");
        }
    });
}

protected void startTimer(){
    int hours;
    int minute;
    int second_hand;

    while (running){
        second++;
        hours = second/3600;
        minute = (second%3600)/60;
        second_hand = second%60;
        String time = String.format("%02d:%02d;%02d", hours, minute, second_hand);

        display.setText(time);

        try{
            Thread.sleep(1000);
        }catch (Exception e){

        }
    }
}
}

以下是LogCat

02-22 00:03:18.207 13038-13038/? I/art: Late-enabling -Xcheck:jni
02-22 00:03:18.357 13038-13038/com.example.nishant.stopwatch I/InstantRun: Instant Run Runtime started. Android package is com.example.nishant.stopwatch, real application class is null.
02-22 00:03:18.447 13038-13038/com.example.nishant.stopwatch V/Monotype: SetAppTypeFace- try to flip, app = com.example.nishant.stopwatch
02-22 00:03:18.447 13038-13038/com.example.nishant.stopwatch V/Monotype:     Typeface getFontPathFlipFont - systemFont = default#default
02-22 00:03:18.457 13038-13038/com.example.nishant.stopwatch V/Monotype: SetAppTypeFace- try to flip, app = com.example.nishant.stopwatch
02-22 00:03:18.457 13038-13038/com.example.nishant.stopwatch V/Monotype:     Typeface getFontPathFlipFont - systemFont = default#default
02-22 00:03:18.767 13038-13038/com.example.nishant.stopwatch W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
02-22 00:03:18.817 13038-13038/com.example.nishant.stopwatch V/Monotype: SetAppTypeFace- try to flip, app = com.example.nishant.stopwatch
02-22 00:03:18.817 13038-13038/com.example.nishant.stopwatch V/Monotype:     Typeface getFontPathFlipFont - systemFont = default#default
02-22 00:03:19.097 13038-13077/com.example.nishant.stopwatch D/OpenGLRenderer: Render dirty regions requested: true
02-22 00:03:19.107 13038-13038/com.example.nishant.stopwatch D/Atlas: Validating map...
02-22 00:03:19.187 13038-13077/com.example.nishant.stopwatch I/OpenGLRenderer: Initialized EGL, version 1.4
02-22 00:03:19.187 13038-13077/com.example.nishant.stopwatch W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
02-22 00:03:19.187 13038-13077/com.example.nishant.stopwatch D/OpenGLRenderer: Enabling debug mode 0
02-22 00:04:46.007 13038-13047/com.example.nishant.stopwatch I/art: Thread[5,tid=13047,WaitingInMainSignalCatcherLoop,Thread*=0xb7ae7f30,peer=0x12c00080,"Signal Catcher"]: reacting to signal 3
02-22 00:04:46.147 13038-13047/com.example.nishant.stopwatch I/art: Wrote stack traces to '/data/anr/traces.txt'

2 个答案:

答案 0 :(得分:2)

尝试以下代码:使用处理程序而不是线程

 public class MainActivity  extends Activity {

    private TextView textTimer;
    private Button startButton;
    private Button pauseButton;
    private long startTime = 0L;
    private Handler myHandler = new Handler();
    long timeInMillies = 0L;
    long timeSwap = 0L;
    long finalTime = 0L;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textTimer = (TextView) findViewById(R.id.textView2);

        startButton = (Button) findViewById(R.id.start);
        startButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                startTime = SystemClock.uptimeMillis();
                myHandler.postDelayed(updateTimerMethod, 0);

            }
        });

        pauseButton = (Button) findViewById(R.id.pause);
        pauseButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                timeSwap += timeInMillies;
                myHandler.removeCallbacks(updateTimerMethod);

            }
        });

    }

    private Runnable updateTimerMethod = new Runnable() {

        public void run() {
            timeInMillies = SystemClock.uptimeMillis() - startTime;
            finalTime = timeSwap + timeInMillies;

            int seconds = (int) (finalTime / 1000);
            int minutes = seconds / 60;
            seconds = seconds % 60;
            int milliseconds = (int) (finalTime % 1000);
            textTimer.setText("" + minutes + ":"
                    + String.format("%02d", seconds) + ":"
                    + String.format("%03d", milliseconds));
            myHandler.postDelayed(this, 0);
        }

    };

}

答案 1 :(得分:2)

您在主线程中使用无限循环。这使得应用程序无法工作。考虑将手表提取到另一个这样的线程:

public class TimerThread extends Thread {
    boolean running;

    int second;
    int hours;
    int minute;
    int second_hand;
    private Handler h; //Because when updating Views, you must do so from the mainThread

    public TimerThread(int seconds) {
        second = seconds;
        running = true;
        h = new Handler();
    }

    protected void startTimer(){

        while (running){
            second++;
            hours = second/3600;
            minute = (second%3600)/60;
            second_hand = second%60;
            String time = String.format("%02d:%02d;%02d", hours, minute, second_hand);

            handler.post(new Runnable() {
                    run() {
                        display.setText(time); //You might have to make display final
                    }
                });


            try{
                Thread.sleep(1000);
            }catch (Exception e){

            }
        }
    }

    public void stopTimer(){
        running = false;
    }

    public int getSeconds() {
        return second;
    }
}

在您的活动中:

private TimerThread mTimerThread;
private int seconds;
public void onCreate (...)  {
    ...
    start_button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if (! running && mTimerThread == null) {
                    mTimerThread = new TimerThread(seconds);
                    mTimerThread.start();
                }
            }
        });

    pause_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (running && mTimerThread != null){ 
                    mTimerThread.stopTimer();
                    seconds = mTimerThread.getSeconds();
                }
            }
        });

    reset_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mTimerThread != null)
                    mTimerThread.stopTimer();
                seconds = 0;
                display.setText("");
                display.setHint("00:00:00");
            }
        });