gpt4 book ai didi

android - View 中没有默认构造函数

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:54:35 26 4
gpt4 key购买 nike

我在使用扩展 android.view.View 的类时遇到问题,这很奇怪,因为我在两个不同的项目中做同样的事情,而且只有其中一个项目是一个问题。

两个项目都有一个类:public class ClassName extends View

但是对于其中一个,这一行是红色下划线,给出的信息是

"There is no default constructor available in 'android.view.View' "

我不知道为什么一个有这个问题而另一个没有。当然,它们不是同一个类,但它们都以相同的方式扩展 View 。我认为这将是一个常见问题,但我没有通过谷歌搜索找到任何相关信息,所以我在这里问。感谢您提供的任何帮助!

编辑:

评论让我再次检查,果然,错误的类没有在其类中定义构造函数。我发现有趣的是,即使我去定义了一个构造函数,它仍然给出了相同的错误,直到我添加了参数(Context context,AttributeSet attrs)并在构造函数中添加了一行'super(context,attrs)'。我添加这些是因为它们存在于 worker 类(Class)的构造函数中。所以工作版本是

public DrawingActivity(Context context, AttributeSet attrs) {
super(context,attrs);
}

我的新问题是关于这到底是做什么的。这是从没有错误的类中获取的,该版本最初是从教程中复制的,当时从未考虑过。

最佳答案

删除没有参数的结构。

你的 View 应该只有 3 个构造函数:

public DrawingActivity(Context context, AttributeSet attrs) {
super(context, attrs);

}

public DrawingActivity(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

}
public DrawingActivity(Context context) {
super(context);
}

关于此构造函数,您可以阅读 here .我只能提一下 defStyle:

The default style to apply to this view. If 0, no style will be applied (beyond what is included in the theme). This may either be an attribute resource, whose value will be retrieved from the current theme, or an explicit style resource.

关于android - View 中没有默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27124412/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com