我试图按照本指南选择时间:http://developer.android.com/intl/es/guide/topics/ui/controls/pickers.html
但是我收到了这个错误:
01-11 19:37:58.513:E / AndroidRuntime(2419):致命异常:主要 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(原生方法)
我的班级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");
}
}
当我这样做时:newFragment.show(getFragmentManager(), "timePicker");
它返回一个NullPointer ..
这是我布局的一部分:
<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"
/>
答案 0 :(得分:2)
.show()
中的NPE。如果在调用showTimePickerDialog()
时TimePickerFragment尚未附加到活动,则可能会发生这种情况。
我建议你将showTimePickerDialog()
定义为静态,并使用片段管理器作为参数。目前我觉得在调用此方法之前首先调用new TimePickerFragment()
,然后再次使用此方法。所以你要实例化对象两次,这是不需要的。
public static void showTimePickerDialog(final FragmentManager manager) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(manager, "timePicker");
}
然后您可以从您的活动中致电:
TimePickerFragment.showTimePickerDialog(getFragmentManager());