2012-09-20 7 views
17

Intento establecer programáticamente el atributo AlignParentRight en el botón en RelativeLayout, pero el programa se bloquea con una NullPointerException. Aquí mi código:Establecer programáticamente el atributo AlignParentRight en el botón RelativeLayout no funciona?

//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); 

¿Dónde está mi problema? El error se produce en la línea del programa layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

ACTUALIZACIÓN CON SOLUCIÓN:

Este código funciona para mí:

// 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); 
+2

además del error que ya se ha observado que debe obtener un 'ClassCastException' siguiente:' 'layouth' necesita LinearLayout.LayoutParams' ya que los parametros son para los padres diseño que es un 'LinearLayout' (' layoutv') – zapl

+0

lp.addRule (RelativeLayout.ALIGN_PARENT_RIGHT); –

Respuesta

8

Cuando se llama a b.getLayoutParams(), B no es de un diseño. por lo tanto, no tiene un layoutparams (además, no sabría que está esperando un RelativeLayout.LayoutParams en este punto).

Necesita crear una nueva instancia de LayoutParams, como lo hizo para TextView.

+0

Gracias, eso resolvió mi problema. Actualizo mi primera publicación con el código actualizado. –

+0

np. revise también el comentario de zapl, de hecho hay algo de malo con los formatos para agregar layouth a layoutv – njzk2

15

reemplazar

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

con

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.MATCH_PARENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT); 
+0

¿qué pasa con el alignParentRight/alignParentLeft que se agregará al código @MukeshSoni? – gumuruh

+5

@gumuruh agregue al código de la siguiente manera: layoutParams.addRule (RelativeLayout.ALIGN_PARENT_RIGHT); – Toni

+0

bien. ese es el código: D – gumuruh

Cuestiones relacionadas