开发者

Within OnClickListener it gives - "The constructor ImageView(new View.OnClickListener(){}) is undefined"?

开发者 https://www.devze.com 2023-04-05 18:38 出处:网络
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 o

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号