Android:找不到自定义 LinearLayout 的构造函数
我正在尝试创建一个自定义 LinearLayout (在 Android 上),但是当尝试在我的主 Activity 类中使用它时,我不断收到编译器错误。扩展的 LinearLayout 需要构造函数传递给它的 Context,但我不知道从哪里获取该 Context。我看到的所有示例都显示了从活动传递 this 指针。我做错了什么?
编译器错误
MyApp.java:15: cannot find symbol
symbol: constructor BoardLayout(com.test.program.MyApp)
location: class com.test.program.BoardLayout
BoardLayout board = new BoardLayout(this);
BoardLayout.java
public class BoardLayout extends LinearLayout {
public void BoardLayout(Context context) {
// initialisation code
}
public BoardLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
MyApp.java
public class MyApp extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BoardLayout board = new BoardLayout(this); // Doesn't work!
setContentView(board);
}
}
解决方案:
看起来问题是两个问题。
- 构造函数没有 void 类型(我的粗心错误)。
- 必须调用“super(context)”作为构造函数中的第一条指令。
I'm trying to create a custom LinearLayout (on Android), but I keep getting compiler errors when trying to use it in my main Activity class. The extended LinearLayout needs a Context passed to it by the constructor, but I don't know where to get that Context. All the examples I see show the passing of the this pointer from the Activity. What am I doing wrong?
Compiler Error
MyApp.java:15: cannot find symbol
symbol: constructor BoardLayout(com.test.program.MyApp)
location: class com.test.program.BoardLayout
BoardLayout board = new BoardLayout(this);
BoardLayout.java
public class BoardLayout extends LinearLayout {
public void BoardLayout(Context context) {
// initialisation code
}
public BoardLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
MyApp.java
public class MyApp extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BoardLayout board = new BoardLayout(this); // Doesn't work!
setContentView(board);
}
}
Solution:
Looks like the problem was two issues.
- Constructors don't have type void (careless mistake on my part).
- Had to call "super(context)" as the first instruction in the constructor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您没有在 BoardLayout 中采用 1 个参数的构造函数。正如评论中指出的,第一个不是构造函数而是方法。
请记住,如果您不显式调用 super,Java 会隐式调用不带参数的 super()。由于 LinearLayout 没有不带参数的构造函数,因此您必须显式调用 super 以避免编译错误。
The problem is that you don't have a constructor that takes 1 argument in BoardLayout. As pointed out in the comments, the first isn0t a constructor but a method.
Keep in mind that Java implicitly calls super() with no arguments, if you don't explicitly call super. Since LinearLayout hasn't a constructor that takes no arguments you have to call super explicitly to avoid compilation errors.
我不是 100% 确定,但是您定义的构造函数需要两个参数:
但是您正在调用的构造函数仅传递一个参数:
如果您想调用新的构造函数,您需要传递第二个参数的值:
我不熟悉扩展 LinearLayout,所以我不确定如何为
AttributeSet
获取正确的值I'm not 100% sure, but the constructor you have defined requires two parameters:
However the constructor you are calling is only being passed one parameter:
If you want to call your new constructor you need to pass in a value for the second parameter:
I'm not familiar with extending a LinearLayout so I'm not sure how to get a proper value for the
AttributeSet