Search code examples
javaandroidtimercrashnumberpicker

App is crashing before it starts


I'm creating a simple app with two NumberPickers used to select a certain number of minutes and seconds. There isn't too much code so I will post it here:

public class MainActivity extends Activity implements OnClickListener {

private static final String TAG = "Interval Trainer";
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private Button startButton;
public TextView text;
private final long interval = 1 * 1000;

//Create NumberPickers
NumberPicker numberPicker1 = (NumberPicker) findViewById(R.id.numberPicker1);
NumberPicker numberPicker2 = (NumberPicker) findViewById(R.id.numberPicker2);

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG,"Entering onCreate()");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startButton = (Button) this.findViewById(R.id.button);
    startButton.setOnClickListener(this);
    text = (TextView) this.findViewById(R.id.timer);

    //Set min and max values for NumberPickers
    numberPicker1.setMaxValue(100);
    numberPicker1.setMinValue(0);
    numberPicker2.setMaxValue(59);  //This is the seconds picker
    numberPicker2.setMinValue(0);

    Log.i(TAG,"Exiting onCreate()");
}

@Override
public void onClick(View v) {
    //Calculate total time from NumberPickers in seconds
    long startTime = (numberPicker1.getValue() * 60) + numberPicker2.getValue();

    //Create CountDownTimer with values from NumberPickers
    countDownTimer = new MyCountDownTimer(startTime, interval);
    text.setText(text.getText() + String.valueOf(startTime / 1000));    //should be removed

    if(!timerHasStarted) {
        countDownTimer.start();
        timerHasStarted = true;
        startButton.setText("STOP");
    } else {
        countDownTimer.cancel();
        timerHasStarted = false;
        startButton.setText("RESTART");
    }

    //Disable the NumberPickers after 'Start' is pressed
    numberPicker1.setEnabled(false);
    numberPicker2.setEnabled(false);
}

public class MyCountDownTimer extends CountDownTimer {
    public MyCountDownTimer(long startTime, long interval) {
        super(startTime, interval);
    }

    @Override
    public void onFinish() {
        text.setText("Time's up!");
        //re-enable the NumberPickers once countdown is done
        numberPicker1.setEnabled(true);
        numberPicker2.setEnabled(true);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        text.setText("" + millisUntilFinished / 1000);

        //Changes the value of the NumberPickers after each tick

    }
}
}

I have a feeling the crash is related to me declaring the two NumberPickers outside of any methods (this would automatically make them static?). Originally I had these two lines of code in my onCreate() but since I needed them in my inner class and other methods I moved it outside. Is this what is causing the crash? If so, how do I do this correctly and still have access to numberPicker1 and 2 all around my class (including inner class)?

Thank you!


Solution

  • You cant intiate object like this

    //Create NumberPickers
    NumberPicker numberPicker1 = (NumberPicker) findViewById(R.id.numberPicker1);
    NumberPicker numberPicker2 = (NumberPicker) findViewById(R.id.numberPicker2);
    

    after onCreate only you layout loads so you dont have you object yet initialized. You can initlize you object in onCreate after

    setContentView(R.layout.activity_main);
    

    For example

    // init variable
    NumberPicker numberPicker1 = null;
    NumberPicker numberPicker2 = null;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        setContentView(R.layout.activity_main);
     // After here only your Activity gets the layout objects
    
        numberPicker1 = (NumberPicker) findViewById(R.id.numberPicker1);
        numberPicker2 = (NumberPicker) findViewById(R.id.numberPicker2);
    
    }