1

I'm trying to pick time following this guide: http://developer.android.com/intl/es/guide/topics/ui/controls/pickers.html

But I'm getting this error:

01-11 19:37:58.513: E/AndroidRuntime(2419): FATAL EXCEPTION: main 01-11 19:37:58.513: E/AndroidRuntime(2419): java.lang.NullPointerException 01-11 19:37:58.513: E/AndroidRuntime(2419): at android.app.DialogFragment.show(DialogFragment.java:228) 01-11 19:37:58.513: E/AndroidRuntime(2419): at iuAndroid.Informe$TimePickerFragment.showTimePickerDialog(Informe.java:126) 01-11 19:37:58.513: E/AndroidRuntime(2419): at iuAndroid.Informe$1.onClick(Informe.java:80) 01-11 19:37:58.513: E/AndroidRuntime(2419): at android.view.View.performClick(View.java:4240) 01-11 19:37:58.513: E/AndroidRuntime(2419): at android.view.View$PerformClick.run(View.java:17721) 01-11 19:37:58.513: E/AndroidRuntime(2419): at android.os.Handler.handleCallback(Handler.java:730) 01-11 19:37:58.513: E/AndroidRuntime(2419): at android.os.Handler.dispatchMessage(Handler.java:92) 01-11 19:37:58.513: E/AndroidRuntime(2419): at android.os.Looper.loop(Looper.java:137) 01-11 19:37:58.513: E/AndroidRuntime(2419): at android.app.ActivityThread.main(ActivityThread.java:5103) 01-11 19:37:58.513: E/AndroidRuntime(2419): at java.lang.reflect.Method.invokeNative(Native Method) 01-11 19:37:58.513: E/AndroidRuntime(2419): at java.lang.reflect.Method.invoke(Method.java:525) 01-11 19:37:58.513: E/AndroidRuntime(2419): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 01-11 19:37:58.513: E/AndroidRuntime(2419): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 01-11 19:37:58.513: E/AndroidRuntime(2419): at dalvik.system.NativeStart.main(Native Method)

My class timePicker:

public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{

@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
    // Use the current time as the default values for the picker
    final Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);

    // Create a new instance of TimePickerDialog and return it
    return new TimePickerDialog(getActivity(), this, hour, minute,
            DateFormat.is24HourFormat(getActivity()));
}

@Override
public void onTimeSet(final TimePicker view, final int hourOfDay, final int minute) {
    // TODO Auto-generated method stub

}

public void showTimePickerDialog(final View v) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getFragmentManager(), "timePicker");

}

}

When I do: newFragment.show(getFragmentManager(), "timePicker");
it returns a NullPointer..

And this is the part of my layout:

<TextView 
    android:id="@+id/from_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/from_name"/>

<Button 
    android:id="@+id/timeFrom"
    android:layout_width="fill_parent"
    android:layout_height="0dip"        
    android:layout_weight="2" 
    android:onClick="showTimePickerDialog"
    style="@android:style/Widget.DeviceDefault.Light.Spinner"
    />
 <TextView 
    android:id="@+id/to_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/to_name"/>

 <Button 
    android:id="@+id/timeTo"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="2"
    android:onClick="showTimePickerDialog"
    style="@android:style/Widget.DeviceDefault.Light.Spinner"
    />
1
  • 1
    What's in your iuAndroid.Informe$1.onClick()? Since you seem to be having a non-static factory-like method showTimePickerDialog() (you shouldn't), you're possibly calling it on an badly initialized object. Commented Jan 12, 2015 at 6:48

1 Answer 1

2

NPE in .show() is usually thrown when we pass fragment manager as null. This might happen in your case if TimePickerFragment was not yet attached to the activity in the moment of calling showTimePickerDialog().

I suggest that you define showTimePickerDialog() as static and as argument use Fragment Manager. Currently i have feeling that you first call new TimePickerFragment() before calling this method, and then again in this method. So you are instantiating object twice and this is not needed.

public static void showTimePickerDialog(final FragmentManager manager) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(manager, "timePicker");
}

You can then call from your activity:

TimePickerFragment.showTimePickerDialog(getFragmentManager());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.