错误:此片段应该提供一个默认的构造函数(不带参数的公共构造) [英] Error: This fragment should provide a default constructor (a public constructor with no arguments)

查看:346
本文介绍了错误:此片段应该提供一个默认的构造函数(不带参数的公共构造)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的类扩展 DialogFragment 是这样的:

 公共类DatePickerFragment扩展DialogFragment实现DatePickerDialog.OnDateSetListener {    的EditText editDate;
    私人日历日期时间= Calendar.getInstance();
    私人SimpleDateFormat的dateFormatter =新的SimpleDateFormat(DD MMMM YYYY);    公共DatePickerFragment(EditText上EDITTEXT){
        editDate = EDITTEXT;
    }@覆盖
公共对话框onCreateDialog(捆绑savedInstanceState){
    //使用当前日期作为选择器的默认日期
    最后的日历C = Calendar.getInstance();
    年整型= c.get(Calendar.YEAR);
    INT月= c.get(的Calendar.MONTH);
    INT天= c.get(Calendar.DAY_OF_MONTH);    //创建DatePickerDialog的新实例,并将其返回
    返回新DatePickerDialog(getActivity(),这,年,月,日);}@覆盖
公共无效onDateSet(查看的DatePicker,年整型,诠释monthOfYear,诠释请将dayOfMonth){
        dateTime.set(年,monthOfYear,请将dayOfMonth);
        editDate.setText(dateFormatter
                .format(dateTime.getTime()));
    }}

这是怎么我在活动使用它:

 公共无效showDatePickerDialog(视图v){
        新DatePickerFragment((的EditText)ⅴ).show(getSupportFragmentManager(),日期选择器);
    }

和调用是这样的:

 <的EditText
        机器人:ID =@ + ID / editDate
        安卓的onClick =showDatePickerDialog/>

不过,我总是得到:

 错误:这个片段应该提供一个默认的构造函数(不带参数的公共构造)


解决方案

Android SDK中力量的新版本,你得到一个空的,无参数的构造函数。它现在是一个很好的实践了这一点。
这允许您保存实例状态的线束和Android再现您的片段调用默认的构造函数。

在这种情况下,您有以下解决方案:

首先,创建默认的构造函数:

 公共DatePickerFragment(){}

创建实例,并通过setter方法​​设置的EditText:

  DatePickerFragment片段=新DatePickerFragment();
fragment.setEditText(五); //创建此setter
fragment.show();

由于是的EditText parcelable,你也可以设置参数:

  DatePickerFragment片段=新DatePickerFragment();
束束=新包();
bundle.putExtra(EditText上,ⅴ);
fragment.setArguments(包); // setArguments是已经存在于片段的方法。
fragment.show(getSupportFragmentManager(),的DatePicker);


至于建议,尽量忽略这些错误配置的build.gradle 是这样的:

  lintOptions {
  abortOnError假
  checkReleaseBuilds假
}

这不会在使用碎片非默认构造函数停止应用程序的构建。

My class extends DialogFragment like this:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    EditText editDate;
    private Calendar dateTime = Calendar.getInstance();
    private SimpleDateFormat dateFormatter = new SimpleDateFormat("dd MMMM yyyy");

    public DatePickerFragment(EditText editText) {
        editDate = editText;
    }

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);

}

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        dateTime.set(year, monthOfYear, dayOfMonth);
        editDate.setText(dateFormatter
                .format(dateTime.getTime()));
    }

}

And this is how i am using it in Activity:

public void showDatePickerDialog(View v) {
        new DatePickerFragment((EditText) v).show(getSupportFragmentManager(), "datePicker");
    }

And calling like this:

    <EditText
        android:id="@+id/editDate"
        android:onClick="showDatePickerDialog" />

But I am always getting :

Error: This fragment should provide a default constructor (a public constructor with no arguments)

解决方案

The newer version of Android SDK forces you to get a empty, no-args constructor. It's now a good practice to to this. This allow you to save the instance state into bundle and the Android recreates your fragment calling the default constructor.

In this case, you have the following solutions:

First, create the default constructor:

public DatePickerFragment() {}

Create the instance and set the EditText via setter method:

DatePickerFragment fragment = new DatePickerFragment();
fragment.setEditText(v); // create this setter
fragment.show();

Since EditText is parcelable, you can also set as arguments:

DatePickerFragment fragment = new DatePickerFragment();
Bundle bundle = new Bundle();
bundle.putExtra("EditText", v); 
fragment.setArguments(bundle); // setArguments is a method that already exists in fragments.
fragment.show(getSupportFragmentManager(), "DatePicker");

[EDIT] As suggested, try to ignore these errors configuring build.gradle like this:

lintOptions { 
  abortOnError false 
  checkReleaseBuilds false 
} 

This will not stop the build of your application by using non-default constructor in fragments.

这篇关于错误:此片段应该提供一个默认的构造函数(不带参数的公共构造)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆