我如何使用我的ImageView?

时间:2015-11-26 04:10:39

标签: java android arrays string object

很抱歉,如果这是一个非常基本的问题,但这是我刚刚在世界各地搜索多个小时而没有运气的时候之一...所以,我正在制作一个带有我自己的Button类的按钮,并且我做了一个图像参数:

public class Button{
    int xPos;
    int yPos;
    int width;
    int height;
    String name;

    // Constructor
    public Button(int x, int y, int w, int h, String n, ImageView t)
    {
        xPos = x;
        yPos = y;
        width = w;
        height = h;
        name = n;
    }
}

现在,我通过创建它的对象(将它放在按钮的arraylist中)来调用它,如下所示:

greenView = (ImageView) findViewById(R.id.greencircle);
greenView.setImageBitmap(BitmapFactory.decodeFile("@drawable/bluecircle"));

blueView = (ImageView) findViewById(R.id.bluecircle);
blueView.setImageBitmap(BitmapFactory.decodeFile("@drawable/bluecircle"));




ArrayList<Button> buttons = new ArrayList<Button>();
buttons.add(new Button(85, 184, 100, 100, "greencircle", greenView));
buttons.add(new Button(185, 184, 100, 100, "bluecircle", blueView));

现在的问题是,由于某种原因,我无法执行findViewById。它认为greencircle没有解决,也不是bluecircle。现在,我正在以编程方式完成这些图像,没有涉及布局。

我看到了这个问题的答案:

How to show image using ImageView in Android

我的方法应该有效。出于某种原因,我无法解析ID。我该怎么办?

非常感谢,

Ruchir

2 个答案:

答案 0 :(得分:0)

您的按钮无效。你必须导入android按钮,它是Viewgroup的子类。

import android.widget.Button;


Button b = new Button(this);
    b.setText("Button added dynamically!");
    b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    b.setId(MY_BUTTON);
    b.setOnClickListener(this);

ArrayList<Button> buttons = new ArrayList<Button>();
buttons.add(b);

答案 1 :(得分:0)

在为按钮设置ID之前,您的程序将无法运行。由于您没有使用xml布局,因此可以在程序中设置id。 您可以手动设置ID yourView.setId(); 希望这有帮助

相关问题