Say I have this code, which creates an ImageView and puts it into a linearlayout.
Also I have an OnClick listener that's supposed to, upon click, remove the old imageview and replace it with a new one.
But strangely it shows "The constructor ImageView(new View.OnClickListener(){}) is undefined".
final LinearLayout LinLayBtn = new LinearLayout(this);
ImageView ivBtn = new ImageView(this);
ivBtn = mkatt.makeKey( ivBtn, btnHue, btnSat, buttonScale, buttonScaleCnt, textAdjust, btnTextColor, buttonText, btnOpa, spacingLR, spacingTB);
LinLayBtn.addView(ivBtn);
LinLayBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LinLayBtn.removeAllViews();
ImageView ivBtn = new ImageView(this); // The constructor ImageView(new View.OnClickListener(){}) is undefined
ivBtn = mkatt.makeKey( ivBtn, btnHue, btnSat, buttonScale, buttonScaleCnt, textAdjust, btnTextColor, buttonText, btnOpa, spacingLR, spacingTB);
LinLayBtn.addView(ivBtn);
}
}开发者_如何学JAVA);
Any ideas what to do with it?
Thanks!
this
in the ImageView constructor is the OnClickListener instance. You need to pass it the instance of your Activity class (i.e. the outer class). Say your outer class is called MyActivity
, then you should pass MyActivity.this
to the ImageView constructor
精彩评论