简体   繁体   中英

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. I've found something very similar here: How do I programmatically add buttons into layout one by one in several lines?

When I try to run it I get a NullPointerException at:

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) ?

@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.

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:

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. I hope this helps. :) !

You must enter the ID of the RelativeLayout, not the xml fil name. try with RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.yourRelativeLayoutViewID);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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