1

I'm new to Android and I can't figure out how to dynamically add a button to a pre-existing layout. I patched together some example code I found from another question on SO into a hello world default project.

onCreate method:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Button myButton = new Button(this);
        myButton.setText("Add Me");

        RelativeLayout ll = (RelativeLayout)findViewById(R.id.main_layout);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        ll.addView(myButton, lp);

        setContentView(R.layout.activity_main);
    }

Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:id="@+id/main_layout"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:id="@+id/cat_1"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="26dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:id="@+id/cat_2"
        android:layout_alignBottom="@+id/cat_1"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="65dp"
        android:layout_marginEnd="65dp" />

</RelativeLayout>

The project works when I comment out the adding a button part so I know that's the problem. And I would have pasted some debug information but the 'debug' filter for the logcat was extremely verbose and constantly updating even if the program wasn't running. When I ran it in debug mode the phone displayed an error for a fraction of a second before the message "The app has stopped working" came up.

Markus Rubey
  • 5,153
  • 2
  • 21
  • 17
Jpaji Rajnish
  • 1,491
  • 4
  • 17
  • 35

3 Answers3

5

setContentView should be done right after super. Otherwise anytime you call findViewById will return a null pointer.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button myButton = new Button(this);
    myButton.setText("Add Me");

    RelativeLayout ll = (RelativeLayout)findViewById(R.id.main_layout);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    ll.addView(myButton, lp);
}
mattfred
  • 2,689
  • 1
  • 22
  • 38
1

You're doing findViewById before setting the content view. That means there's no views to find. So when you try to use that view, you get a null pointer exception

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
1
LinearLayout ll = (LinearLayout) findViewById(R.id.main_layout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

for(int i=0;i<30;i++){
    Button myButton = new Button(this);
    myButton.setText("Add Me");
    myButton.setId(i);
    ll.addView(myButton, lp);
}
kRiZ
  • 2,320
  • 4
  • 28
  • 39