gpt4 book ai didi

java - DialogFragment 返回 NullPointer

转载 作者:行者123 更新时间:2023-11-29 20:55:33 25 4
gpt4 key购买 nike

我正尝试按照本指南选择时间:http://developer.android.com/intl/es/guide/topics/ui/controls/pickers.html

但是我收到了这个错误:

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)

我的上课时间选择器:

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"
/>

最佳答案

.show() 中的 NPE 通常在我们将 fragment 管理器作为 null 传递时抛出。如果在调用 showTimePickerDialog() 时 TimePickerFragment 尚未附加到 Activity ,则您的情况可能会发生这种情况。

我建议您将 showTimePickerDialog() 定义为静态的,并作为参数使用 Fragment Manager。目前我觉得你在调用这个方法之前先调用 new TimePickerFragment() ,然后在这个方法中再次调用。所以你要实例化对象两次,这是不需要的。

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

然后您可以从您的 Activity 中调用:

TimePickerFragment.showTimePickerDialog(getFragmentManager());

关于java - DialogFragment 返回 NullPointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27893825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com