17

I try to programmatically set the attribute AlignParentRight to button in RelativeLayout but the program crashes with a NullPointerException. Here my code:

//add horizontal realative layout
RelativeLayout layouth = new RelativeLayout(this);
layouth.setLayoutParams(new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

//add textview for label
TextView t = new TextView(this);
t.setText(d.getName());
t.setTextSize(25);
t.setId(2);      
t.setLayoutParams(new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

//add button
Button b = new Button(this);
b.setText(d.getName());
RelativeLayout.LayoutParams layoutParams =(RelativeLayout.LayoutParams)b.getLayoutParams();
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
layoutParams.addRule(RelativeLayout.LEFT_OF, 2);
b.setLayoutParams(layoutParams);   

//add textview and button to horizontal linear layout
layouth.addView(t);
layouth.addView(b);
//add horizontal linear layout to vertical linear layout
layoutv = (LinearLayout) findViewById(R.id.ProjectViewLinearID);
layoutv.addView(layouth);

Where is my problem? The error occurs in the program line layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

UPDATE WITH SOLUTION:

This code works for me:

// Creating a new RelativeLayout
RelativeLayout layouth = new RelativeLayout(this);

// Defining the RelativeLayout layout parameters.
// In this case I want to fill its parent
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT,
        RelativeLayout.LayoutParams.MATCH_PARENT);

// Creating a new TextView
TextView tv = new TextView(this);
tv.setText(d.getName());
tv.setTextSize(25);

//add button
Button b = new Button(this);
b.setText(d.getName());

// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
         RelativeLayout.LayoutParams.WRAP_CONTENT,
         RelativeLayout.LayoutParams.WRAP_CONTENT);
         lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

// Setting the parameters on the TextView
tv.setLayoutParams(lp);
b.setLayoutParams(lp);

// Adding the TextView to the RelativeLayout as a child
layouth.addView(tv);
layouth.addView(b);
Onik
  • 19,396
  • 14
  • 68
  • 91
Mokkapps
  • 2,028
  • 9
  • 42
  • 67
  • 2
    besides the error you observed already you should get a `ClassCastException` next: `layouth` needs `LinearLayout.LayoutParams` since the params are for the parent layout which is a `LinearLayout` (`layoutv`) – zapl Sep 20 '12 at 09:29
  • lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); – jeet.chanchawat Apr 29 '13 at 12:08

2 Answers2

14

replace

RelativeLayout.LayoutParams layoutParams =(RelativeLayout.LayoutParams)b.getLayoutParams();

with

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
Mukesh Soni
  • 6,646
  • 3
  • 30
  • 37
  • 5
    @gumuruh add to the code as follows: layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); – Toni Jul 12 '15 at 20:12
8

When you call b.getLayoutParams(), b is not in a layout. therefore, it does not have a layoutparams (plus, it wouldn't know that you are expecting a RelativeLayout.LayoutParams at this point).

You need to create a new LayoutParams instance, like you did for the TextView.

njzk2
  • 38,969
  • 7
  • 69
  • 107