单击按钮时尝试创建imageView

单击按钮时尝试创建imageView

本文介绍了单击按钮时尝试创建imageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击按钮时我试图创建ImageView.我不知道为什么在创建它时会在新Image上收到错误消息.

I am trying to create ImageView when I click the button.I don't know why when I am creating it I get the error on new Image.

我正在工作的代码如下:

the code I am working is as follows:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button save;

    save = (Button) findViewById(R.id.savebtn);

    save.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            ImageView image = new ImageView(activity_main,this);
            RelativeLayout mylayout = (RelativeLayout) findViewById(R.id.container);
            image.setBackgroundResource(R.drawable.element_wall);
            mylayout.addView(image);

        }
    });

就像我说的那样,我在这条线上出现错误: ImageView图片=新的ImageView(activity_main,this);我试图只放这个但不起作用,我正在使用activity_main.

As I sayd I am getting error on this line: ImageView image = new ImageView(activity_main,this);I have tryed to put only this but not working, I am using activity_main.

推荐答案

我不知道您发布的错误,正确或输入错误,但根据您的代码,应该是

I don't know that you posted wrong or right or typing mistake but according to your code it should be

 ImageView image = new ImageView(activity_main,this);

 ImageView image = new ImageView(activity_main.this);

您也可以使用

 image.setImageResource(R.drawable.element_wall);

代替

 image.setBackgroundResource(R.drawable.element_wall);

这篇关于单击按钮时尝试创建imageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:30