动态地将RelativeLayout添加到LinearLayout

时间:2014-10-30 12:31:42

标签: android dynamic android-linearlayout relativelayout creation

我正在尝试动态创建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);上得到一个NULL指针异常,知道为什么?

2 个答案:

答案 0 :(得分:2)

您应该更改订单

 setContentView(R.layout.activity_main);

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

首先setContentView(...),然后初始化您的布局。

答案 1 :(得分:1)

NULL pointer exception occur因为你无法实例化mainLO。参见下面的代码: -

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