0

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

Community
  • 1
  • 1
  • I think your problem cannot reach your game.xml . can you provide your structure(outline) please? – guness Apr 10 '12 at 12:11
  • 1
    i'm using an array of ints to simulate the minesweeper field. for example a value of 9 at position[2] indicates that there is a mine at position 2 on the minesweeper field. then i'm using if statements to generate different buttons i.e a button that represents a mine will be created if position[2] == 9. I'm trying to add these buttons to a relative layout that will represent the minesweeper field. does that help? – DanielFitzgerald123 Apr 10 '12 at 13:36

3 Answers3

2

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.

Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
  • yes i have set the content view, I changed the reference to the ID rather than the file name itself but I still can't see any buttons when the activity starts. i've got the call to the create() method in the onCreate() method, is any call that's put in the onCreate() method called when the activity starts? – DanielFitzgerald123 Apr 10 '12 at 13:31
  • no longer get a nullpointerexception. if i call the create() method the screen just stays blank, no buttons or anything – DanielFitzgerald123 Apr 11 '12 at 10:45
2

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. :) !

Ange
  • 309
  • 2
  • 5
  • 14
0

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

VinceFR
  • 2,551
  • 1
  • 21
  • 27