1

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.
1
  • are you importing your BoardLayout class? Commented Jan 3, 2011 at 22:57

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The first one isn't actually a constructor as it has a return type and is therefore a method.
ah yes. sry i missed that. But then the problem is even clearer. There is no BoardLayout constructor that takes 1 argument ;)
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.