BottomSheetDialogFragment

时间:2017-08-03 10:05:36

标签: java android android-fragments nullpointerexception fragment

我试图用一个带有标记标记的新片段替换地图片段。但是,在我单击替换Fragment的日期后,我有一个空指针。有帮助吗?感谢。

  custom = new CustomDateTimePicker(getActivity(),
            new CustomDateTimePicker.ICustomDateTimeListener() {

                @Override
                public void onSet(Dialog dialog, Calendar calendarSelected,
                                  Date dateSelected, int year, String monthFullName,
                                  String monthShortName, int monthNumber, int date,
                                  String weekDayFullName, String weekDayShortName,
                                  int hour24, int hour12, int min, int sec, String AM_PM) {
                    DecimalFormat df = new DecimalFormat("00");
                    String i_hr = df.format(hour24);
                    String i_min = df.format(min);
                    String i_sec = df.format(sec);
                    String i_monthNumber = df.format(dateSelected.getMonth()+1);
                    String i_calendar_day = df.format(dateSelected.getDay()-1);

                    dateFrom = (i_monthNumber)
                            + "/" + (i_calendar_day) + "/" + year
                            + i_hr + ":" + i_min
                            + ":" + i_sec;

                        SnailTrailFragment stf;
                        stf = new SnailTrailFragment();
                    FragmentManager manager;
                        manager = manager = getFragmentManager();
                    manager.beginTransaction().replace(R.id.mainLayout, stf).commit();

                }

堆栈错误跟踪

  

显示java.lang.NullPointerException                                                        at com.journeytech.mark.mark.BottomSheetModalFragment $ 2 $ 1.onSet(BottomSheetModalFragment.java:88)                                                        在com.journeytech.mark.mark.CustomDateTimePicker.onClick(CustomDateTimePicker.java:278)                                                        在android.view.View.performClick(View.java:4560)                                                        在android.view.View $ PerformClick.run(View.java:18636)                                                        在android.os.Handler.handleCallback(Handler.java:733)                                                        在android.os.Handler.dispatchMessage(Handler.java:95)                                                        在android.os.Looper.loop(Looper.java:136)                                                        在android.app.ActivityThread.main(ActivityThread.java:5021)                                                        at java.lang.reflect.Method.invokeNative(Native Method)                                                        在java.lang.reflect.Method.invoke(Method.java:515)                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:827)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)                                                        在dalvik.system.NativeStart.main(本地方法)

1 个答案:

答案 0 :(得分:0)

您需要使用支持片段管理器和AppCompatActivity

final FragmentManager manager = getSupportFragmentManager();

CustomDateTimePicker custom = new CustomDateTimePicker(getActivity(),
        new CustomDateTimePicker.ICustomDateTimeListener() {

             ... 
             Fragment stf = new SnailTrailFragment();
             manager.beginTransaction()
                .replace(R.id.mainLayout, stf)
                .commit();