2012-09-20 10 views
17

Provo a impostare in modo programmatico l'attributo AlignParentRight sul pulsante in RelativeLayout ma il programma si arresta in modo anomalo con una NullPointerException. Qui il mio codice:Imposta programmaticamente l'attributo AlignParentRight sul pulsante in RelativeLayout non funziona?

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

Dove è il mio problema? L'errore si verifica nella riga di programma layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

AGGIORNAMENTO CON SOLUZIONE:

Questo codice funziona per 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); 
+2

oltre l'errore si osserva già si dovrebbe ottenere un 'ClassCastException' prossimo:' 'layouth' bisogno LinearLayout.LayoutParams' dal momento che i parametri sono per il genitore layout che è un 'LinearLayout' (' layoutv') – zapl

+0

lp.addRule (RelativeLayout.ALIGN_PARENT_RIGHT); –

risposta

8

Quando si chiama b.getLayoutParams(), b non è in un layout. quindi, non ha un layoutparam (in più, non saprebbe che ti aspetti un RelativeLayout.LayoutParams a questo punto).

È necessario creare una nuova istanza di LayoutParams, come per il TextView.

+0

Grazie, questo ha risolto il mio problema. Aggiorno il mio primo post con il codice aggiornato. –

+0

np. controlla anche il commento di zapl, c'è in effetti qualcosa di sbagliato nel layoutparam per l'aggiunta di layouth a layoutv – njzk2

15

sostituire

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

con

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

che dire di alignParentRight/alignParentLeft da aggiungere nel codice @MukeshSoni? – gumuruh

+5

@gumuruh aggiungere al codice come segue: layoutParams.addRule (RelativeLayout.ALIGN_PARENT_RIGHT); – Toni

+0

ok. questo è il codice: D – gumuruh

Problemi correlati