错误:这个类应该提供一个默认构造函数(一个没有参数的公共构造函数)[可以实例化] 在Android中。

6

我正在使用Android Studio中的“生成已签名APK”来生成apk文件,但是出现了以下错误:

错误:错误:此类应提供默认构造函数(一个没有参数的公共构造函数)(com.actua.actuaconsultores.actuamindfulness.GridImagenAdaptar)[可实例化]

我的类如下:

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

什么是问题?
谢谢

4
建议的更改不应该是必要的,这意味着有些地方出了问题。您是否在AndroidManifest.xml文件中无意间引用了这个类? - CommonsWare
不确定 getIDForResourceName 的作用是什么,但你不需要使用反射来完成它。 - OneCricketeer
4个回答

6

这个错误意味着你需要添加:

public GridImagenAdaptar(){}

使用构造函数将其添加到您的类中。


如果这回答了你的问题,我很高兴能够帮助。你可以回报一下,接受这个答案。 - Sunshinator

6

请检查 AndroidManifest.xml 文件,可能会意外添加一行类名,请删除或更改该类名,然后问题就会得到解决。例如,在我的情况下,我添加了 ParyllService,我将其删除以使其正常工作。

 <!--  was uncessary <service
        android:name=".services.finance.payroll.PayrollService"
        android:enabled="true"
        android:exported="true"></service>-->

同时,如果你的类中有[Service]注解,它也会达到同样的效果,所以如果有的话,请将其删除,这样问题就会得到解决。 - Simas Paškauskas

3
解决方案是将默认值设为类参数的默认值(而不是删除它们)。 我在Kotlin类中遇到了同样的问题,因为这个Activity类必须在AndroidManifest.xml中,并且还需要一个参数(用于列表详细处理的ID号)。清单文件中的错误是相同的:This class should provide a default constructor (a public constructor with no arguments),甚至无法运行应用程序。解决问题的唯一方法就是在构造函数参数中添加默认值(并在类中进行一些处理):
class ArticleDetail(id: Long): AppCompatActivity() {...

进入

class ArticleDetail(id: Long = -1): AppCompatActivity() {

0
定义一个公共的空构造函数,只需添加以下代码:

public GridImagenAdaptar(){}; 在 ArrayList 声明之后


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接