-2

我想要動態創建一個RelativeLayout(動態添加視圖)並將其添加到動態創建的LinearLayout。下面的代碼:動態添加一個RelativeLayout到一個LinearLayout

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Log.d("TAG", "ON CREATE"); 

    LinearLayout mainLO = (LinearLayout) findViewById(R.id.linearLayout); 
    Log.d("TAG", "LINEAR LAYOUT CREATED"); 

    setContentView(R.layout.activity_main); 
    Log.d("TAG", "CONTENT SET"); 

    relLO = new RelativeLayout(this); 
    RelativeLayout.LayoutParams lpMatchParent = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.MATCH_PARENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 
    lpMatchParent.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
    relLO.setBackgroundColor(0xBBBBBBBB); 
    relLO.setId(10); 
    Log.d("TAG", "relLO CREATED"); 

    btn1 = new Button(this); 
    btn1.setText("Btn1"); 
    btn1.setId(1); 
    btn1.setOnClickListener(this); 
    btn1Created = true; 
    Log.d("TAG", "BUTTON ONE CREATED"); 

    btn2 = new Button(this); 
    btn2.setText("Btn2"); 
    btn2.setId(2); 
    btn2.setOnClickListener(this); 
    btn2Created = true; 
    Log.d("TAG", "BUTTON TWO CREATED"); 

    btn3 = new Button(this); 
    btn3.setText("Btn3"); 
    btn3.setId(3); 
    btn3.setOnClickListener(this); 
    btn3Created = true; 
    Log.d("TAG", "BUTTON THREE CREATED"); 

    lpWrapContent = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 
    lpWrapContent.addRule(RelativeLayout.CENTER_HORIZONTAL); 
    lpWrapContent.setMargins(10, 2, 10, 2); 
    Log.d("TAG", "lpWrapContent CREATED"); 

    lpWrapContentRight = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 
    lpWrapContentRight.addRule(RelativeLayout.RIGHT_OF, btn1.getId()); 
    lpWrapContentRight.setMargins(10, 2, 10, 2); 
    Log.d("TAG", "lpWrapContentRight CREATED"); 

    lpWrapContentLeft = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 
    lpWrapContentLeft.addRule(RelativeLayout.LEFT_OF, btn1.getId()); 
    lpWrapContentLeft.setMargins(10, 2, 10, 2); 
    Log.d("TAG", "lpWrapContentLeft CREATED"); 

    relLO.addView(btn1, lpWrapContent); 
    relLO.addView(btn2, lpWrapContentRight); 
    relLO.addView(btn3, lpWrapContentLeft); 
    Log.d("TAG", "VIEWS ADDED"); 

    mainLO.addView(relLO, lpMatchParent); 
    Log.d("TAG", "relLO ADDED TO LINEAR LAYOUT"); 
} 

我得到一個空指針異常就上線mainLO.addView(relLO, lpMatchParent);,任何想法,爲什麼?

回答

2

您應該更改順序

setContentView(R.layout.activity_main); 

LinearLayout mainLO = (LinearLayout) findViewById(R.id.linearLayout); 

首先setContentView(...)然後初始化佈局。

1

NULL pointer exception occur,因爲你不能實例下面的代碼mainLO.See: -

protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
LinearLayout mainLO = (LinearLayout) findViewById(R.id.linearLayout); 
+0

如果我沒有記錯的其他代碼我上次設定的內容來看,並沒有產生一個NULL指針。爲什麼在這種情況下會發生? – 2014-10-30 12:37:57

+0

您必須在setContentView bcz之後實例化視圖,然後才能使用或實例化xml的所有視圖。@ RodrigoRutsatz – duggu 2014-10-30 12:41:31

相關問題