0

I really do not understand why this simple code crashes in this line layout.addView(button);

super.onCreate(savedInstanceState);
    layout = (LinearLayout)findViewById(R.id.linearLayout);
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

    Button button = new Button(this);
    button.setHeight(100);
    button.setWidth(100);
    button.setText("HELLO");
    button.setLayoutParams(p);
    layout.addView(button);

activity_main.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearLayout">
</LinearLayout>

EDIT: edited code still crushes. But now crushes in setContentView.

super.onCreate(savedInstanceState);

    layout = (LinearLayout)findViewById(R.id.linearLayout);
    setContentView(layout); 
    LayoutParams p = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);

    int i,j;
    Button button = new Button(this);
    button.setHeight(100);
    button.setWidth(100);
    button.setText("HELLO");
    layout.addView(button,p);

Logcat report enter image description here

5
  • you have to call setContentView BEFORE findViewById not AFTER Commented Apr 8, 2013 at 15:04
  • Then how am I gonna give the layout as argument in setContentView? I have to get it from the xml first right by doing findViewById? Commented Apr 8, 2013 at 15:06
  • 3
    can't you simply do setContentView(R.layout.activity_main) ? Commented Apr 8, 2013 at 15:07
  • Thanks blackbelt it works :) But suppose activity_main.xml had two or three layouts. And I wanted to get only one of them as my layout for the activity. how can I do that? Commented Apr 8, 2013 at 15:12
  • 1
    I am glad it works. your question does not make sense to me. Commented Apr 8, 2013 at 15:16

2 Answers 2

5

Simply because you haven't called setContentView(). The root View of your Activity is null, so is layout.

Sign up to request clarification or add additional context in comments.

4 Comments

@kalkin, You need to call setContentView() before doing any findViewById() calls.
usually the call looks like setContentView(R.layout.yourLayout), the value of the layout variable is probably not a valid layout id
but I added that id in the activity_main.xml. And if I do setContentView(R.layout.yourLayout) then after that how am I gonna access yourlayout from java code so that later I can add buttons and stuff in it?
This is just example code. You better read some basics on how android works - maybe here: developer.android.com/training/basics/firstapp/index.html
0

You're specifying the Views width height here:

button.setHeight(100);
button.setWidth(100);

then immediately overwriting it:

button.setLayoutParams(p);

try this, remove the setWidth and setLayoutParams and remove p:

  layout.addView(button, new LayoutParams(100,100));

3 Comments

Hi I edited as you mentioned. But it crushes in the setContentView. thanks.
Edited my answer, sorry, missed one of your lines in the question
... Ah.. "Crushes" - you mean "Crashes" I thought the button was being crushed. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.