我試圖將選定的日期值設置爲edittext。我得到以下錯誤從外部類設置edittext值時出現錯誤
顯示java.lang.NullPointerException:嘗試上的空對象引用
public static class AddDataFragment extends BaseFragment implements {
@BindView(R.id.tvStartVisit)
public TextView tvStartVisit;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add_visit, container, false);
ButterKnife.bind(this, view);
setData();
return view;
}
@OnClick(R.id.tvStartVisit)
public void onStartVisitDateClicked(){
DialogFragment newDialogFragment = new DatePickerFragment();
newDialogFragment.show(getActivity().getSupportFragmentManager(), "datePicker");
}
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@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);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
AddVisitFragment mAddVisitFragmentForDatePicker = new AddDataFragment();
mAddVisitFragmentForDatePicker.tvStartVisit.setText(year+"-"+month+"-"+day);
}
}
}
在DatePickerFragment(TextView的tvStartVisit){ this.tvStartVisit = tvStartVisit獲得誤差; } –