This seems like an elementary problem so I'm sure it's something small I've overlooked. Maybe I've just been looking at it too long.
I've been trying to create an onClick Listener in a view, and I have a public set method to set the listener. But when I try to call that method outside of the class I get an error saying it can't resolve the method.
I've tried creating other public methods or public member variables but I can't view any of them outside of the class for some reason.
Here is some relative snippets:
PaintView: (The class I can't get public members from)
public class PaintView extends View {
public interface OnPaintClickedListener
{
public void onPaintClicked(int color);
}
private OnPaintClickedListener _onPaintClickedListener;
public void setOnPaintClickedListener(OnPaintClickedListener listener)
{
_onPaintClickedListener = listener;
}
...
}
PaletteView: (The class that uses a PaintView)
public class PaletteView extends ViewGroup {
....
public void addColor(Context context, int color)
{
View newPaintView = new PaintView(context, color);
//setOnPaintClickedListener gives the message "Cannot resolve method setOn...blah blah'
newPaintView.setOnPaintClickedListener(new PaintView.OnPaintClickedListener()
{
@Override
public void onPaintClicked(int color)
{
}
});
this.addView(newPaintView);
}
}
It does ok with the interface code, but it just can find that setOnPaintClickedListener method.
Thanks in advance, I'm 97% sure I'm going to feel like an idiot as soon as somebody points out my mistake.
Change to:
PaintView newPaintView = new PaintView(context, color);
explanation...as the variable you were using was declared with type View, the compiler wasn't able to find the method that was defined in the subclass PaintView, so it complains.
Alternatively, you could leave the same declaration and when calling the specific method, you would have to do cast to the subclass like this:
((PaintView)newPaintView).setOnPaintClickedListener
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With