Note that there are some explanatory texts on larger screens.

plurals
  1. PONullPointerException when setText to EditText - onDateSet
    primarykey
    data
    text
    <p>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:</p> <p>Start class:</p> <pre><code>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"); } } </code></pre> <p>DatePick Class:</p> <pre><code> 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); } } </code></pre> <p>also XML:</p> <pre><code> &lt;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" &gt; &lt;requestFocus /&gt; &lt;/EditText&gt; </code></pre> <p>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. </p> <p>EDIT: </p> <pre><code> 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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload