简体   繁体   English

创建一个按钮并以编程方式将其添加到视图

[英]Create a button and add it to a view programmatically

I'm creating a version of the popular Minesweeper game for Android. I'm trying to programmatically create a button and add it to a RelativeLayout.我正在为 Android 创建一个流行的扫雷游戏版本。我正在尝试以编程方式创建一个按钮并将其添加到 RelativeLayout。 I've found something very similar here: How do I programmatically add buttons into layout one by one in several lines?我在这里发现了一些非常相似的东西: How do I programmatically add buttons into layout one by one by several lines?

When I try to run it I get a NullPointerException at:当我尝试运行它时,我在以下位置收到 NullPointerException:

RelativeLayout layout1 = (RelativeLayout) findViewById(R.layout.game);

Here's the whole block of code:这是整个代码块:

public void create() {
    RelativeLayout layout1 = (RelativeLayout) findViewById(R.layout.game);
    for(int i = 0; i < gridSize; i++) {
        if(grid[i] == 0) { //if grid pos. indicates an empty cell
            Button empty = new Button(this);
            empty.setBackgroundResource(R.drawable.emptybutton); //set background to empty
            empty.setId(i); //set id to value of i
            empty.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            layout1.addView(empty); //add the button to the relativeLayout view
            //((Button) findViewById(i)).setOnClickListener(emptyListener); 
        }

Thanks in advance for any responses提前感谢您的任何回复

have set the layout xml of the Activity by setContentView(R.layout.xxxx) ?已通过setContentView(R.layout.xxxx)设置 Activity 的布局 xml?

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);


...

this这个

 RelativeLayout layout1 = (RelativeLayout) findViewById(R.layout.game);

should be应该

 RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.relative_id);

R.id... used for mapping control and RelativeLayout is a control. R.id...用于映射控件,RelativeLayout是一个控件。

I think you're getting a blank screen because you haven't set the content view.我认为您的屏幕是空白的,因为您尚未设置内容视图。 What i mean is that the codes does what it's supposed to do however, you should remove the "setContentView()" method at the top and place it at the end, then you should set it to the RelativeLayout before you close the onCreate() method: Something like this:我的意思是代码做了它应该做的但是,你应该删除顶部的“setContentView()”方法并将它放在最后,然后你应该在关闭 onCreate() 之前将它设置为 RelativeLayout方法:像这样:

public void create() {
RelativeLayout layout1 = new RelativeLayout(this);
for(int i = 0; i < gridSize; i++) {
    if(grid[i] == 0) { //if grid pos. indicates an empty cell
        Button empty = new Button(this);
        empty.setBackgroundResource(R.drawable.emptybutton); //set background to empty
        empty.setId(i); //set id to value of i
        empty.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        layout1.addView(empty); //add the button to the relativeLayout view
        //((Button) findViewById(i)).setOnClickListener(emptyListener); 
     }
    }
     setContentView(layout1);
   }

Also notice that I've changed the declaration of the Relativelayout a little bit.另请注意,我稍微更改了 Relativelayout 的声明。 I hope this helps.我希望这有帮助。 :) ! :) !

You must enter the ID of the RelativeLayout, not the xml fil name.您必须输入 RelativeLayout 的 ID,而不是 xml 文件名。 try with RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.yourRelativeLayoutViewID);尝试使用 RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.yourRelativeLayoutViewID);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM