1

I want to create a layout in Android in a java class. I need that the user introduce a number between 1 to 20 and this create the number of butttons that the user has chosen.I want to create dynamic buttons and I have this code:

package com.example.nuevo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

public class ControladorResuelto extends Activity {


protected Controlador controlador;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     requestWindowFeature(Window.FEATURE_NO_TITLE);
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);


        LinearLayout buttonsLayout =  (LinearLayout)findViewById(R.id.linearlayoutUp); 

    int nMallas = controlador.getnMallas();

    LinearLayout.LayoutParams layoutParams = new     LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    for(int i=0;i<nMallas;i++){
        Button button = new Button(this);
        button.setText("Buttin "+(i+1));
        buttonsLayout.addView(button, layoutParams);******
    }

    setContentView(R.layout.panel_resuelto);
}

I have an error at the line with asterisks

2
  • what's the specific error message? NPE? Commented Feb 24, 2014 at 17:21
  • Yes Embattled Swag, it was that but now I have another problem, the 3 buttons that I have created dynamically were in a vertical layout and now I have changed the linear Layout to horizontal and they don't appear do you what is happening? Commented Feb 24, 2014 at 17:30

1 Answer 1

1

I'm pretty sure it's because you have setContentView(R.layout.panel_resuelto); at the end of your onCreate. Put it at the beginning, else findViewById won't work. I'm guessing you're getting a null pointer exception because the LinearLayout couldn't successfully be created since the content view wasn't set yet.

Sign up to request clarification or add additional context in comments.

5 Comments

More specifically, findViewById won't work until setContentView is created in this case.
Yes Embattled Swag, it was that but now I have another problem, the 3 buttons that I have created dynamically were in a vertical layout and now I have changed the linear Layout to horizontal and they don't appear do you what is happening?
I can't really tell. I would suggest creating a new question (with the full code).
here there is my XML layout and the java code is the same with the CotentView changed yet.
Thanks Embattled Swag!! I posted here my XML layout stackoverflow.com/questions/21995038/…

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.