Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error:This class should provide a default constructor (a public constructor with no arguments) [Instantiatable] in Android

I am generating the file apk with Generated Signed APK in Android Studio, but I have this error:

Error:Error: This class should provide a default constructor (a public constructor with no arguments) (com.actua.actuaconsultores.actuamindfulness.GridImagenAdaptar) [Instantiatable]

my class is:

public class GridImagenAdaptar extends BaseAdapter {

    private final Context mContext;
    private final ArrayList<ModelImage> mGridItems;

    public GridImagenAdaptar(Context mContext, ArrayList<ModelImage> mGridItems) {
        this.mContext = mContext;
        this.mGridItems = mGridItems;
    }

    public int getCount() {
        return mGridItems.size();
    }

    public Object getItem(int position) {
        return mGridItems.get(position);
    }

    public long getItemId(int position) {
        return position;
    }


    public View getView(int position, View convertView, ViewGroup parent)
    {
        if( convertView == null)
        {   // If ReusedView doesn't exist
            convertView = inflatedViewWithContextInParentView(R.layout.activity_grid_imagen_adaptar, mContext, parent);

            convertView.setTag("HomeMenuCell"); // Reuse Identifier
        }

        fillViewWithItemAtIndex(convertView, position);

        return convertView;
    }

    private View inflatedViewWithContextInParentView(int id, Context context, ViewGroup parentView)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
        View inflatedView = inflater.inflate(id, parentView, false);
        return inflatedView;
    }

    private void fillViewWithItemAtIndex(View reusedView, int index)
    {
        ModelImage item = mGridItems.get(index);
        item.setId(index);

        TextView title = (TextView) reusedView.findViewById(R.id.title);
        title.setText(item.title);

        ImageView picture = (ImageView) reusedView.findViewById(R.id.image);
        int resourceID = getIDForResourceName(item.imageName);
        picture.setImageResource(resourceID);
    }

    private int getIDForResourceName(String name)
    {
        try {
            Class res = R.drawable.class;
            Field field = res.getField(name);
            int drawableId = field.getInt(null);
            return drawableId;
        }
        catch (Exception e) {
            Log.e("GridItemsAdapter", "Error: Couldn't get a drawable id.", e);
        }
        return -1;
    }
}

What is the problem?

Thanks

like image 923
elvega Avatar asked Jun 14 '16 20:06

elvega


3 Answers

Check it out in AndroidManifest.xml file there must be additional line of class accidently added just remove or change that class name then it will be fine. For example in my case it was ParyllService added like this and I removed to make it working.

 <!--  was uncessary <service
        android:name=".services.finance.payroll.PayrollService"
        android:enabled="true"
        android:exported="true"></service>-->
like image 123
Ishwor Khanal Avatar answered Oct 16 '22 15:10

Ishwor Khanal


This error means you have to add:

public GridImagenAdaptar(){}

to your class, with your constructors.

like image 21
Sunshinator Avatar answered Oct 16 '22 15:10

Sunshinator


Solution could be also a default value for class parameters (instead of deleting them). I had the same issue with Kotlin class that had to be in AndroidManifest.xml, because it was Activity class and in addition the class required parameter (ID number for list-detail processing). The error in manifest was the same This class should provide a default constructor (a public constructor with no arguments) and it was not possible even to run the app. The only thing needed to solve the issue was to add default value to the constructor parameters (and handle it a little in the class):

class ArticleDetail(id: Long): AppCompatActivity() {...

into

class ArticleDetail(id: Long = -1): AppCompatActivity() {
like image 3
Lukas Avatar answered Oct 16 '22 16:10

Lukas