I have a custom view MyView
inflated from my_view.xml
(with a corresponding Java class MyView.java
).
I override MyView(Context context, AttributeSet attrs)
to connect subviews up to members of the class.
Now one of these subviews is a Button
, and I'd like for my view to listen for a click on its button before passing this event on to a delegate. However if I declare
this.myButton.setOnClickListener(this);
in the constructor MyView(Context context, AttributeSet attrs)
(where this
is an instance of MyView
) I get a NullPointerException
.
Where is an appropriate place in MyClass.java
to call this.myButton.setOnClickListener(this);
?
%%
Edit. Some code:
public MyView(Context ctx, AttributeSet attrs)
{
super(context, attrs);
this.myButton = (Button) this.findViewById(R.id.my_button);
this.myButton.setOnClickListener(this); // Raises null pointer;'id' is correct.
}