0

so I have an activity with radiobuttons a datepicker and a timepicker that appear when you click two different buttons. Then I have another button underneath that puts text in a textview with the radio button selected, the day and time displayed on the bottom of the screen. The application stops running when I click the third button. The timepicker and the datepicker work just fine but when I click on the third button it crashes.

 package net.androidbootcamp.shepherdschurchapp;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.text.DateFormat;
import java.util.Calendar;

public class Study extends AppCompatActivity {

private TextView reservation;
private String time = " ";
private String day = " ";


@Override
protected void onCreate(Bundle savedInstanceState) {

    //VARIABLES FOR RADIO BUTTONS
    final RadioButton oneOnOne = 
(RadioButton)findViewById(R.id.oneonone);
    final RadioButton smallGroup = 
(RadioButton)findViewById(R.id.smallgroup);
    final RadioButton largeGroup = 
(RadioButton)findViewById(R.id.largegroup);


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_study);

    //VIEW ICON LAUNCHER
    ActionBar actionBar = getSupportActionBar();
    actionBar.setLogo(R.mipmap.ic_launcher);
    actionBar.setDisplayUseLogoEnabled(true);
    actionBar.setDisplayShowHomeEnabled(true);
    //


    reservation = 
(TextView)findViewById(R.id.txtReservation);
    Button button1 = (Button)findViewById(R.id.btnDate);
    Button button2 = (Button)findViewById(R.id.btnTime);
    Button convert = (Button)findViewById(R.id.appointment);

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new TimePickerDialog(Study.this, t, 
b.get(Calendar.HOUR_OF_DAY), b.get(Calendar.MINUTE), 
false).show();
        } //end onClick
    }); //End setOnClickListener


    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new DatePickerDialog(Study.this, d, 
c.get(Calendar.YEAR), c.get(Calendar.MONTH), 
c.get(Calendar.DAY_OF_MONTH)).show();
        } //end onClick
    }); //End setOnClickListener


    convert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {



            if(oneOnOne.isChecked()) {

                reservation.setText("Your one-on-one bible 
study is set for: " + day + " at " + time + ".");

            }

            if(smallGroup.isChecked()) {

                reservation.setText("Your small group bible 
study is set for: " + day + " at " + time + ".");

            }

            if(largeGroup.isChecked()) {

                reservation.setText("Your large group bible 
study is set for: " + day + " at " + time + ".");

            }

        }
    });

} // END ONCREATE

Calendar c = Calendar.getInstance();
Calendar b = Calendar.getInstance();
DateFormat fmtDate = DateFormat.getDateInstance();

DatePickerDialog.OnDateSetListener d = new 
DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int 
month, int dayOfMonth) {
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, dayOfMonth);

        day = fmtDate.format(c.getTime());



    }
};


TimePickerDialog.OnTimeSetListener t = new 
TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, 
int minute) {
        b.set(Calendar.HOUR_OF_DAY, hourOfDay);
        b.set(Calendar.MINUTE, minute);
        String ampm;
        int hour = hourOfDay;

        if (hourOfDay > 12) {
            hour -= 12;
        }

        if (hourOfDay >= 12) {
            ampm = "PM";

        }
        else {
            ampm = "AM";
        }


        if(hourOfDay < 7|| hourOfDay > 23){
            Toast.makeText(Study.this, "We only do bible 
study between 7 am and 11 pm", Toast.LENGTH_LONG).show();

        }
        else {
            time = hour + ":" + minute +  " " + ampm;

        }


    }

};




}
Alexandra
  • 125
  • 1
  • 11
  • Look at [the stack trace](https://stackoverflow.com/questions/23353173) to determine the cause of the crash. – Mike M. Dec 13 '18 at 02:27
  • I see it. You need to move all of the `findViewById()` calls to after `setContentView()`. You currently have all the `RadioButton` initializations before that call. – Mike M. Dec 13 '18 at 02:33
  • No problem. I kinda jumped the gun, here, 'cause I was 99% sure of the problem, but for future questions concerning crashes, please do include the stack trace, so we can be certain. Anyhoo, glad it helped. Cheers! – Mike M. Dec 13 '18 at 02:47
  • I couldn't see the stack trace for some reason it was blank. Do you know how to access it from when it is blank? – Alexandra Dec 13 '18 at 02:48
  • Totally blank usually means it's not connected to a device, either virtual or real. Look at the dropdown on the top-left of the log window, and make sure you've got the right device selected there. If you do, then check the rest of the options there – the process, filter text, etc. – to make sure your logs aren't being filtered out. – Mike M. Dec 13 '18 at 02:52
  • I have a new question to help with my classmate, if you could possibly help? @MikeM. – Alexandra Dec 13 '18 at 03:53

0 Answers0