public class TimePickerClass extends Activity{
public TimePicker mTimePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_picker_class);
Button saveTimeButton;
mTimePicker = (TimePicker) findViewById(R.id.timePicker);
mTimePicker.setIs24HourView(true);
saveTimeButton = (Button) findViewById(R.id.next_button);
saveTimeButton.setOnClickListener(v -> {
getTime();
});
public String getTime(){
//TODO
int hour;
int minute;
hour = mTimePicker.getHour();
minute = mTimePicker.getMinute();
String message;
message = "Time:" + hour +" : " + minute;
return message;
}
Hello! I have a class in which using TimePicker I get the time chosen by the user. I'm trying to do this:
TimePickerClass time = new TimePickerClass();
String userTime = time.getTime();
But this does not work, the application is down. How then can this be realized? The error is as follows:
FATAL EXCEPTION: main
Process: com.gfc.glanceclock, PID: 32256
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.TimePicker.getHour()' on a null object reference