Question

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)
Was it helpful?

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.

OTHER TIPS

This line

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

is giving you error as you can't find your EditText in DatePicker. You will need to pass your EditText in the constructor for DatePick or somehow pass the value back to it in its orignal Activity

Create a constructor for you DatePicker class and pass the EditText to it. For example:

public DatePickerFragment (EditText editText) {
    this.editText = editText;
}

and then simply use it in onDateSet() method.

Use

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

instead of

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

Because, you don't have edittext in your DatePick dialog.. Because of this only findViewById() returning null..

What if you remove view from this function?

public void onDateSet(DatePicker view, int year, int month, int day) {
    // Do something with the date chosen by the user
    //so instead of:
    //editText = (EditText) view.findViewById(R.id.etDepDate);
    //use this:
    editText = (EditText) findViewById(R.id.etDepDate);
    editText.setText(day + "/" + month + "/" + year);
}

You can't access EditText inside DatePick class. You need to send the text back to Start from DatePick class. you can do it this way.

  1. Define an interface and callback method in DatePick class.

    callbackListener listener;
    
    interface callbackListener {
    public void setText(String text);
    }
    
  2. Implement the interface in Start class.

    class Start extends FragmentActivity implements DatePick.callbackListener {
    
    .....
    
    @Override
    public void setText(String text) {
    
    EditText editText = (EditText) view.findViewById(R.id.etDepDate);
    editText.setText(text);
    

    }

  3. In onAttach method of DatePick class assign the original activity to the interface object.

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        listener = (callbackListener) activity;
    }
    

4.Call the callback method in onDateSet method and pass the text

public void onDateSet(DatePicker view, int year, int month, int day) {
    listener.setText(day + "/" + month + "/" + year);
}

Since your Edittext is in Fragment Activity and not in the DatePicker DialogFragment You cannot access it directly in this way. Try creating a reference to your fragment activity and access it through a method. Declare Edittext's scope to be accessed across in this activity. Then,

public void setDate(String Dte){
  editText.setText(Dte);
}

And just reconstruct your DialogFragment onDateSet method like this.

public void onDateSet(DatePicker view, int year, int month, int day) {
 // Do something with the date chosen by the user
 ((MainActivity)getActivity()).setDate(day + "/" + month + "/" + year);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top