java—如何添加默认构造函数

8ftvxx2r  于 2021-07-13  发布在  Java
关注(0)|答案(6)|浏览(427)

希望我没有在这里重复一个问题,但我在stack exchange上找到的所有问题似乎都不符合我的需要。
以下是我的代码片段:

public class custom_row_adapter extends ArrayAdapter<Integer> {
     custom_row_adapter(Context context, ArrayList<Integer> picId, String url, String fullUrl) {
         super(context, R.layout.activity_custom_row_adapter, picId);
}
     //Do stuff

在应用程序中一切正常,现在我尝试生成一个签名的apk,我得到一个很好的小错误,我需要一个默认的构造函数。所以我决定添加一个,现在我的代码如下所示:

public class custom_row_adapter extends ArrayAdapter<Integer> {
    public custom_row_adapter() {
        super();
}
     custom_row_adapter(Context context, ArrayList<Integer> picId, String url, String fullUrl) {
         super(context, R.layout.activity_custom_row_adapter, picId);
}
//Do Stuff

现在我得到一个新的错误,它找不到合适的构造函数 ArrayAdapter() 所以我在google上找到了几个帖子,其中一个告诉我,我可以绕过这个错误,在inspections标签中搜索instantiable,然后把它变成警告,警告它会修复它。没用。
那我该怎么解决这个问题呢?提前谢谢。
编辑:对于那些说我可以添加 public custom_row_adpater(){}
编辑2:这里还有一些图片,请注意左下角的错误:
调试工作原理:

保罗·g的回答是:

解除构造函数:

无构造函数:

oipij1gg

oipij1gg1#

这个错误意味着出于某种原因lint(错误地)认为适配器应该是可示例化的。这适用于可能由系统示例化的对象,如活动、广播接收器,但不适用于适配器,因为它们总是在应用程序的代码中示例化。例如,常用的适配器 ArrayAdapter 或者 SimpleCursorAdapter 没有默认构造函数。
你可以试着让androidstudio恢复理智,比如更新构建工具,清理工作区,删除 build 目录。。。如果不起作用,您可以添加请求的(无用的)默认构造函数:

public custom_row_adapter() {
    super(null , 0);
    throw new RuntimeException("This is a workaround and should not be used");
}
envsm3lx

envsm3lx2#

这是因为arrayadapter没有定义无参数构造函数。这就是编译器在不带参数调用super()时所寻找的。它至少需要一个上下文和一个资源(int)来初始化自己。

r1zk6ea1

r1zk6ea13#

arrayadapter没有默认的零参数构造函数,因此必须重写其中一个构造函数。你可以在这里找到更多信息android-没有arrayadapter的默认构造函数

public class custom_row_adapter extends ArrayAdapter<Integer> { 
    public custom_row_adapter(Context context, int resource) {
        super(context, resource); 
    }
ecfdbz9o

ecfdbz9o4#

由于您正在扩展arrayadapter,因此可以从这里示例化您的对象:

Custom_row_adapter customAdapter = new Custom_row_adapter(this, android.R.layout.simple_spinner_item, insertIntegerArray);

如果希望类中有一些额外的参数,请创建一个自定义构造函数:

public Custom_row_adapter(Context context, int resource, Integer[] array, arg extraArg) {
    super(context, resource, array);
    this.extraArg = extraArg;
 }

我的建议是把你们班改名为 Custom_row_adapter . 用大写字符开始类名是一个很好的做法。

mqxuamgl

mqxuamgl5#

来自javase文档:
如果构造函数体不是以显式构造函数调用开始的,并且所声明的构造函数不是原始类对象的一部分,那么构造函数体将以超类构造函数调用“super();”隐式开始,对其直接超类的构造函数的调用,不带参数。
为默认构造函数提供一个空的构造函数体与放置一个超级调用是一样的。

public custom_row_adapter() {
    super();
}

与相同

public custom_row_adapter() {}

因此它会导致一个错误,因为在超类中没有默认构造函数,并且需要显式构造函数调用。在这种情况下,可以重载构造函数并用这个(args)调用它,或者用超(args)调用某个超类构造函数。
参考文献:http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.8.7

clj7thdc

clj7thdc6#

我希望我能理解你的要求。

解决方案

因为 ArrayAdapter 没有默认的“no arg”构造函数,必须提供一些参数。i、 而不是把

public custom_row_adapter() {
    super();
}

相反,您需要指定(至少)两个参数。有关更多信息,请参阅javadocs。

public custom_row_adapter() {
    super(context, resource);
}

什么 context 以及 resource 需要你来决定。因为您可能不需要这个构造函数,所以它们可能是 null 还有一些 int ,就像 0 或者 -1 .

为什么

默认情况下,当没有为类提供构造函数时,java会自动构建一个什么都不做的构造函数(本质上)。但是,一旦创建了另一个构造函数,默认的构造函数( custom_row_adapter() )“消失”。有关更多信息,请参阅此帖子。
另外,默认情况下,no arg构造函数的第一个调用( custom_row_adapter() )含蓄地 super(); (参见java se)。自 ArrayAdapter 没有默认的无参数构造函数,这是无效的,必须为其提供参数。

相关问题