I'm using Android's DatePicker to select a certain date from the calendar. The problem is I don't manage to show the date I select from the calendar in my EditText.
This would be my DatePickerFragment
:
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private EditText a;
private String date;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
a.findViewById(R.id.in_date);
date = "";
date = Integer.toString(day) + "/" + Integer.toString(month) + "/" + Integer.toString(year);
a.setText(date);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
}
and this is the xml of the EditText I'm using:
< EditText
android: id = "@+id/in_date"
android: layout_width = "175dp"
android: layout_height = "wrap_content"
android: hint = "@string/choose_date"
android: importantForAutofill = "no"
android: inputType = "date|text"
android: keyboardNavigationCluster = "false" / >
Every time I get NullPointerException
when trying to use .setText(date)
on the EditText variable a
. Isn't this the way it should be initialized?
You're not initializing the variable a
which means it is null
. You're getting the NullPointer
because in the background, it is doing (null).setText(date);
EditText a = (EditText) findViewById(R.id.in_date);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.