0

EDIT: I have found the problem and it is the Thread.Sleep is there any other way of making my application wait a second?

I'm trying to learn android development so I'm using android studio. I have an activity, which isn't the main activity and I tried to build a timer that starts counting from 40 minutes when the activity starts, but for some reason when I press the button in the main activity that is supposed to change the activity to the one with the timer, the app crashes. This is the timer's activity code:

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;


public class Timer extends Activity {

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

}

@Override
protected void onStart() {
        String counter;
        int totalSeconds = 2400;
        int minLeft, secLeft;
        for (int i = totalSeconds; i > 0; i--)
        {
            try
            {
                Thread.sleep(1000L);
            }
            catch (InterruptedException e) {e.printStackTrace();}
            minLeft=(int)Math.floor(i/60);
            secLeft=i-(minLeft*60);
            counter = minLeft+":"+secLeft;
            TextView tv = (TextView)findViewById(R.id.timer);
            tv.setText(counter);
        }
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.quiz, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}
ril2
  • 55
  • 2
  • 6

2 Answers2

1

You need to move all of your counter logic off of your main thread. Try something like this:

private int secondsLeft = 2400;

private Handler mHandler = new Handler();

public void onStart() {
    super.onStart();

    final TextView tv = (TextView)findViewById(R.id.timer);

    mHandler.postDelayed(new Runnable() {

        public void run() {
            secondsLeft--;
            int minLeft = (int)Math.floor(secondsLeft / 60);
            int secLeft = secondsLeft - (minLeft * 60);
            tv.setText(minLeft + ":" + secLeft);

            if (secondsLeft > 0)
                mHandler.postDelayed(this, 1000);
        }

    }, 1000);
Dan Harms
  • 4,725
  • 2
  • 18
  • 28
  • @MarcoAcierno I decided you were right and have made an edit to my answer. Also, added a neat little trick for doing an action each second without a continuous thread. You can still keep track of the `Handler` if you want to cancel it if needed. – Dan Harms Apr 12 '14 at 14:29
0

You probably missed to call super.onStart() in your overridden onStart()

Denis Loh
  • 2,194
  • 2
  • 24
  • 38
  • That didn't solve it but I added it to the code anyway, I found the problem which is the sleep. any idea on how I can make it work? – ril2 Apr 12 '14 at 14:05
  • @user3135114 Put it in an async task, or better A handler thread. This moves it from your main ui thread. They are both pretty straight forward with examples. – zgc7009 Apr 12 '14 at 14:14