0

I have an edittext that shows current date. onclick of that edittext, datepicker dialog pops up. However After setting the date in dialog, it throws nullpointer Exception. I am inserting my code below. I tried with DialogFragment as showdialog is depreciated. Can anyone help me where i went wrong in my code?

    import java.util.Calendar;
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.DatePickerDialog;
    import android.app.Dialog;
    import android.app.DialogFragment;
    import android.view.View;
    import android.widget.DatePicker;
    import android.widget.EditText;

public class setDate extends Activity {
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.datepicker);

    EditText dateValue = (EditText) findViewById(R.id.date);
    final Calendar c = Calendar.getInstance();
    int yearText = c.get(Calendar.YEAR);
    int monthText = c.get(Calendar.MONTH);
    int dayText = c.get(Calendar.DAY_OF_MONTH);

    dateValue.setText(dayText + "-" + (monthText + 1) + "-" + yearText);

}

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "datePicker");

}

public static class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    public Dialog onCreateDialog(Bundle savedInstanceState) {

        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);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {

        String a = String.valueOf(day);
        String b = String.valueOf(month + 1);
        String c = String.valueOf(year);

        View id = new Activity().findViewById(R.id.date);
        EditText dateValue = (EditText) id;
        dateValue.setText(a + "-" + b + "-" + c);

    }

}
}

since findViewById is undefined in its current class, i instantiated for Activity and tried to use findViewById.

cruise
  • 23
  • 4
  • http://stackoverflow.com/questions/18211684/how-to-transfer-the-formatted-date-string-from-my-datepickerfragment/18212061#18212061. instead of textview use editext – Raghunandan Oct 15 '13 at 15:21
  • @Raghunandan I got the output. Thanks for your links. – cruise Oct 15 '13 at 15:40
  • I used `View id=getActivity().findViewById(R.id.date);` instead of `View id = new Activity().findViewById(R.id.date);` then i got the output – cruise Oct 15 '13 at 15:42

0 Answers0