Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 729
Oreo Avatar asked Apr 21 '15 04:04

Oreo


People also ask

Why is it recommended to use only the default constructor to create a fragment?

Traditionally, a Fragment instance could only be instantiated using its default empty constructor. This is because the system would need to reinitialize it under certain circumstances like configuration changes and the app's process recreation.

Can default constructor have parameters?

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .

What is the primary purpose of a default constructor?

What is the primary job of a constructor? to initialize the attributes of an object.


1 Answers

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() {}

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.

like image 100
Deividi Cavarzan Avatar answered Oct 15 '22 07:10

Deividi Cavarzan