Search code examples
javaandroidandroid-dialogfragmentandroid-datepicker

NullPointerException when setText to EditText - onDateSet


I have an editText, where when the user clicks it, a date Dialog appears, and when date is set, the date should appear in the editText field. Although simple enough, I can 't find why I get a nullPointer Exception. I have tried several ways and changed my code a lot, but nothing works. Here it is one of the concepts of my code:

Start class:

public class Start extends FragmentActivity {

int year;
int month;
int day;

static final int DATE_DIALOG_ID = 999;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);
    EditText editText = (EditText) findViewById(R.id.etDepDate);
    editText.setText("date");

}

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

}

}

DatePick Class:

 public class DatePick extends DialogFragment implements
    DatePickerDialog.OnDateSetListener {
EditText editText;

@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) {
    // Do something with the date chosen by the user
    editText = (EditText) view.findViewById(R.id.etDepDate);
    editText.setText(day + "/" + month + "/" + year);
}

@Override
public void show(FragmentManager manager, String tag) {
    // TODO Auto-generated method stub
    super.show(manager, tag);
}
}

also XML:

 <EditText
     android:id="@+id/etDepDate"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:ems="10"
    android:focusable="false"
    android:hint="@string/dhint"
    android:inputType="date"
    android:onClick="showDatePickerDialog" >

    <requestFocus />
</EditText>

I had made it work with another method, but it is deprecated and I don 't want to use it. DialogFragments seems the right way to do it.

EDIT:

 11-14 20:38:06.368: E/AndroidRuntime(31557): FATAL EXCEPTION: main
 11-14 20:38:06.368: E/AndroidRuntime(31557): java.lang.NullPointerException
11-14 20:38:06.368: E/AndroidRuntime(31557):    at        com.xxx.mytrips.DatePick.onDateSet(DatePick.java:36)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at     android.app.DatePickerDialog.tryNotifyDateSet(DatePickerDialog.java:148)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at android.app.DatePickerDialog.onClick(DatePickerDialog.java:116)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at android.os.Looper.loop(Looper.java:137)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at android.app.ActivityThread.main(ActivityThread.java:5041)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at java.lang.reflect.Method.invokeNative(Native Method)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at java.lang.reflect.Method.invoke(Method.java:511)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at dalvik.system.NativeStart.main(Native Method)

Solution

  • It's because you're using editText = (EditText) view.findViewById(R.id.etDepDate);. findViewById returns null in this case because editText is not a child of the DatePicker view.

    If the editText is part of the content view of your activity you can use this :

    editText = (EditText) getActivity().findViewById(R.id.etDepDate);

    This is not really good practice by the way. Preferably you would pass an external OnDateSetListener to the fragment.