Android:找不到自定义 LinearLayout 的构造函数

发布于 2024-10-10 04:41:33 字数 1137 浏览 0 评论 0原文

我正在尝试创建一个自定义 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);
    }
}

解决方案:

看起来问题是两个问题。

  1. 构造函数没有 void 类型(我的粗心错误)。
  2. 必须调用“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.

  1. Constructors don't have type void (careless mistake on my part).
  2. Had to call "super(context)" as the first instruction in the constructor.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮华 2024-10-17 04:41:33

问题是您没有在 BoardLayout 中采用 1 个参数的构造函数。正如评论中指出的,第一个不是构造函数而是方法。

public class BoardLayout extends LinearLayout {
   public void BoardLayout(Context context) {
      // initialisation code
   }
   public BoardLayout(Context context, AttributeSet attrs) {
      super(context, attrs);
   }
}

请记住,如果您不显式调用 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.

public class BoardLayout extends LinearLayout {
   public void BoardLayout(Context context) {
      // initialisation code
   }
   public BoardLayout(Context context, AttributeSet attrs) {
      super(context, attrs);
   }
}

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.

笑,眼淚并存 2024-10-17 04:41:33

我不是 100% 确定,但是您定义的构造函数需要两个参数:

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

但是您正在调用的构造函数仅传递一个参数:

BoardLayout board = new BoardLayout(this);

如果您想调用新的构造函数,您需要传递第二个参数的值:

BoardLayout board = new BoardLayout(this,null);

我不熟悉扩展 LinearLayout,所以我不确定如何为 AttributeSet 获取正确的值

I'm not 100% sure, but the constructor you have defined requires two parameters:

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

However the constructor you are calling is only being passed one parameter:

BoardLayout board = new BoardLayout(this);

If you want to call your new constructor you need to pass in a value for the second parameter:

BoardLayout board = new BoardLayout(this,null);

I'm not familiar with extending a LinearLayout so I'm not sure how to get a proper value for the AttributeSet

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文