1

Can't work out a way to make an array of buttons in android.

This is the code I have tried but I'm getting a java.lang.NullPointerException.

    private Button[] button = {(Button) findViewById(R.id.cGuess1),
        (Button) findViewById(R.id.cGuess2),(Button)
        findViewById(R.id.cGuess3),(Button) findViewById(R.id.cGuess4)};

Is this even possible?

EDIT:

Sorry everyone. Just realised my mistake!

I was trying to declare the array for my whole class and trying to get the views from the ids before onCreate so there was no setContentView(R.layout.game);

Sorry.

5
  • yes, possible . what error or unexpected result you are facing ??? share detailed code Commented Jan 9, 2012 at 12:55
  • Well all I'm getting is the "java.lang.NullPointerException" on the line shown. Not even got far enough to even work with the array. Commented Jan 9, 2012 at 12:56
  • 1
    please post the full exception from your LogCat, it could be that your view has not been inflated Commented Jan 9, 2012 at 12:57
  • 1
    this means one more more button is missing among cGuess1,2,3 and 4 . cant say more before have a look of xml and java Commented Jan 9, 2012 at 13:05
  • Well that's great you have realized your mistake. But accept or upvote the comments and answers that helped you. Commented Jan 9, 2012 at 13:29

5 Answers 5

2

Since no one else posted actual code for a solution, here's a working snippet.

Button[] myButtons = null;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    myButtons = new Button[]
    {
            (Button)findViewById(R.id.button1),
            (Button)findViewById(R.id.button2),
            (Button)findViewById(R.id.button3),
    };
}
Sign up to request clarification or add additional context in comments.

Comments

1

Just a guess as full code is not available here, have you called setContentView() before creating array.

Comments

1

Could you try

final Button[] button = {(Button) findViewById(R.id.cGuess1), 
    (Button) findViewById(R.id.cGuess2),(Button) 
    findViewById(R.id.cGuess3),(Button) findViewById(R.id.cGuess4)};

Comments

1

One of your buttons may be null. And putting a private keyword does not allow me to create the array. Also see that Firstly you are setting the cententView for your activity and then accessing these buttons.

Comments

0
public class main2 extends Activity{
    final int[] button = {R.id.button1,R.id.button2,R.id.button3,R.id.button4,R.id.button5,
            R.id.button6,R.id.button7,R.id.button8,R.id.button9,R.id.button10};
    Button[] bt = new Button[button.length];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign);
        for(int i=0;i<button.length;i++){
            final Context context = this;
            final int b = i;
            bt[b]= (Button) findViewById(button[b]);
            Typeface font = Typeface.createFromAsset(getAssets(), "Angkor.ttf");
            bt[b].setTypeface(font);
            bt[b].setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent myIntent = new Intent(context,r1.class);
                    startActivity(myIntent);
                }
            });
        }
    }
}

2 Comments

Hi @PhearumChheang, could you explain your answer please? Code without comment is less helpful than well commented code that others can learn from. Thank you.
Sorry about some mistakes. This code is for have many buttons and want to loop it for use only one purpose. For example, you want to start new activity by click on button whatever you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.