2012-09-20 15 views
17

我嘗試以編程方式將屬性AlignParentRight設置爲RelativeLayout中的按鈕,但程序崩潰時發生NullPointerException。在這裏我的代碼:在RelativeLayout中以編程方式將屬性AlignParentRight設置爲按鈕不起作用?

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

我的問題在哪裏?在程序行出現的錯誤與Solution layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

UPDATE:

此代碼的工作對我來說:

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

除了你已經注意到你應該得到一個'ClassCastException'錯誤下一頁:'layouth'需要'LinearLayout.LayoutParams'因爲PARAMS是父佈局是'LinearLayout'('layoutv') – zapl

+0

lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); –

回答

8

當你調用b.getLayoutParams(),b未在佈局。因此,它沒有layoutparams(另外,它不會知道您期望在此處使用RelativeLayout.LayoutParams)。

您需要創建一個新的LayoutParams實例,就像您爲TextView所做的那樣。

+0

謝謝,這解決了我的問題。我用更新的代碼更新我的第一篇文章。 –

+0

np。檢查zapl評論,事實上佈局界面有什麼問題將layout添加到layoutv – njzk2

15

更換

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

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

那麼alignParentRight/alignParentLeft要添加到代碼@MukeshSoni中呢? – gumuruh

+5

@gumuruh添加到代碼如下:layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); – Toni

+0

好的。這是代碼:D – gumuruh

相關問題