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

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

问题描述

我的类像这样扩展DialogFragment:

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");
    }

然后这样调用:

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

但我总是得到:

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

推荐答案

较新版本的 Android SDK 强制您获得一个空的、无参数的构造函数.现在这样做是一个很好的做法.这允许您将实例状态保存到包中,Android 将调用默认构造函数重新创建您的片段.

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

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

In this case, you have the following solutions:

首先创建默认构造函数:

First, create the default constructor:

public DatePickerFragment() {}

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

Create the instance and set the EditText via setter method:

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

由于 EditText 是可分块的,您也可以设置为参数:

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");

按照建议,尝试忽略这些配置 build.gradle 的错误,如下所示:

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天全站免登陆