Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

java - Trying to set text of EditText from DatePicker DialogFragment

I am simply trying to get the date from a datepicker dialog I created from one of the many android datepicker tutorials I found online. I seem to be going wrong somewhere in the actual retrieving of the text date once it is selected. I have a DatePickerFragment class, which is called from a CreateEvent fragment which is nested inside my MainActivity activity class. This is the DatePicker:

public class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    private EditText txtDate;
    @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);
        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        txtDate = (EditText)view.findViewById(R.id.txtDate);
        txtDate.setText(day + " " + (month + 1) + " " + year);
    }
}

I get Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence) on a null object reference when I actually try and set the date, which I understand means it can't actually find the edittext element in my create event fragment xml file.

How should I go about setting the text of this edittext element from onDateSet or do I have to change my approach?

I tried it this way but to no avail.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have done this by below lines of code:

public class DatePickerDialogMy extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        DateSetting dateSetting=new DateSetting(getActivity());
        Calendar calendar= Calendar.getInstance();
        int year= calendar.get(calendar.YEAR);
        int month=calendar.get(calendar.MONTH);
        int day=calendar.get(calendar.DAY_OF_MONTH);
        DatePickerDialog dialog;
        dialog=new DatePickerDialog(getActivity(),dateSetting,year,month,day);
        return dialog;
    }
}

Second Class:

        public class DateSetting implements android.app.DatePickerDialog.OnDateSetListener {
            Context context;
            public DateSetting(Context context){
                this.context=context;

            }
            @Override
            public void onDateSet(DatePicker view, int year, int dateSetting, int dayOfMonth) {
                Toast.makeText(context, "selected date:" + dateSetting + "/" + dayOfMonth + "/" + year, Toast.LENGTH_LONG).show();
        //        MainActivity.test.setText(String.valueOf(dateSetting));
                MyActivity.dobEditText.setText(dateSetting+"/"+dayOfMonth+ "/" + year);
            }
        }

How to call:

 DatePickerDialogMy datePickerDialog = new DatePickerDialogMy();
                datePickerDialog.show(getSupportFragmentManager(), "date_picker");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...