首页 > 解决方案 > 片段 null 必须是一个公共静态类,才能从运行应用程序的实例状态正确重新创建

问题描述

运行应用程序时出现此错误

片段 null 必须是公共静态类才能从实例状态正确重新创建

关于 newfragment.show 声明。

我在 DatePickerFragment 上得到了 lint,要求是静态的,但是是静态的……有什么建议吗?我已经检查了其他类似的问题,但不帮助我

// picker calendar
    birthdate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            DialogFragment newFragment = new  DatePickerFragment() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int day) {
                    String birthdatestr = (day + "/" + (month + 1) + "/" + year);
                    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy", Locale.US);

                    Date datebirth = null;
                    try {
                        datebirth = formatter.parse(birthdatestr);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    DateFormat f = 
DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault());
                    String formattedDate = f.format(datebirth);
                    birthdate.setText(formattedDate);
                    if (datebirth != null) {
                        birthmills = datebirth.getTime();
                    }
                }


            };

            newFragment.show(getFragmentManager(), "Date Picker");

        }

    });
}



public static class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    public String data;



    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = getInstance();
        int year = c.get(YEAR);
        int month = c.get(MONTH);
        int day = c.get(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

    }

    public String getData() {

        return data;
    }
}

标签: android

解决方案


推荐阅读