【问题标题】:Error:This class should provide a default constructor (a public constructor with no arguments) [Instantiatable] in Android错误:此类应在Android中提供默认构造函数(不带参数的公共构造函数)[Instantiatable]
【发布时间】:2016-06-14 20:13:02
【问题描述】:

我在 Android Studio 中使用 Generated Signed APK 生成文件 apk,但出现此错误:

错误:错误:此类应提供默认构造函数(不带参数的公共构造函数)(com.actua.actuaconsultores.actuamindfulness.GridImagenAdaptar) [Instantiatable]

我的班级是:

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

有什么问题?

谢谢

【问题讨论】:

  • 建议的更改不应该是必要的,这表明有问题。您是否不小心在AndroidManifest.xml 文件中引用了这个类?
  • 不完全确定 getIDForResourceName 的用途,但您不需要反思。

标签: android constructor baseadapter


【解决方案1】:

AndroidManifest.xml 文件中检查它,一定是意外添加了额外的类行,只需删除或更改该类名就可以了。例如,在我的情况下,它是像这样添加的 ParyllService,我删除了它以使其正常工作。

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

【讨论】:

  • 另外——你的类中的[Service]注解实现了同样的事情,所以如果你有它——删除它,它会解决问题
【解决方案2】:

这个错误意味着你必须添加:

public GridImagenAdaptar(){}

到你的班级,和你的构造函数。

【讨论】:

  • 如果这回答了您的问题,我很高兴能为您提供帮助。您可以通过接受答案来回报您的帮助。
【解决方案3】:

解决方案也可以是类参数的默认值(而不是删除它们)。 我对必须在 AndroidManifest.xml 中的 Kotlin 类有同样的问题,因为它是 Activity 类,另外还有类必需参数(用于列表详细信息处理的 ID 号)。清单中的错误是相同的此类应提供默认构造函数(没有参数的公共构造函数),甚至无法运行应用程序。唯一需要解决的问题就是为构造函数参数添加默认值(并在类中稍微处理一下):

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

进入

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多