[英]I can't figure out what's wrong with my NullPointerException or why it even exists
[英]Can't figure out why I get this NullPointerException on my EditText
我正在使用 Android 的 DatePicker 從日歷中選擇某個日期。 問題是我無法在 EditText 中顯示從日歷中選擇的日期。
這將是我的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);
}
}
這是我正在使用的 EditText 的 xml:
< 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" / >
每次我嘗試在 EditText 變量a
上使用.setText(date)
時得到NullPointerException
。 這不是應該初始化的方式嗎?
您沒有初始化變量a
這意味着它是null
。 你得到NullPointer
因為在后台,它正在做(null).setText(date);
EditText a = (EditText) findViewById(R.id.in_date);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.