Skip to content
Advertisement

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

My class extends DialogFragment like this:

JavaScript

And this is how i am using it in Activity:

JavaScript

And calling like this:

JavaScript

But I am always getting :

JavaScript

Advertisement

Answer

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:

JavaScript

Create the instance and set the EditText via setter method:

JavaScript

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

JavaScript

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

JavaScript

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

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement