7

I created button in activity programmaticaly, not in xml file. Then I wanted to set it LayoutParams like in this link: How to programmatically set the layout_align_parent_right attribute of a Button in Relative Layout?

But when I tried to launch, I had an exception.

Here is my code:

RelativeLayout ll2 = new RelativeLayout(this);
    //ll2.setOrientation(LinearLayout.HORIZONTAL);

    ImageButton go = new ImageButton(this);
    go.setId(cursor.getInt(cursor.getColumnIndex("_id")));
    go.setClickable(true);
    go.setBackgroundResource(R.drawable.go);
    RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams)go.getLayoutParams();
    params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); // LogCat said I have Null Pointer Exception in this line
    go.setLayoutParams(params1);
    go.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i3 = new Intent(RecipesActivity.this, MedicineActivity.class);
            i3.putExtra(ShowRecipe, v.getId());
            i3.putExtra("Activity", "RecipesActivity");
            RecipesActivity.this.startActivity(i3);
        }
    });
    ll2.addView(go);

Why my app throws an exception? Thanks.

1
  • Probably the line above the line LogCat pointed as null pointer is coming null but to help you it's better you update your question posting the error. Commented Aug 29, 2013 at 21:54

3 Answers 3

26

Change this:

RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams)go.getLayoutParams();
params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
go.setLayoutParams(params1);

for:

RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
go.setLayoutParams(params1);

When you create a View programatically it doesn't have any LayoutParams, that's why you are getting NullPointerException. If you inflate the view from XML, the view is coming now with LayoutParams.

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

1 Comment

LayoutParams.MATCH_PARENT need to be set as RelativeLayoutParams.MATCH_PARENT, since there are multiple types of LayoutParams
1

From what I see, it looks like

go.getLayoutParams()

returns null in this line

 RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams)go.getLayoutParams();

because you have no LayoutParams set for it. This will obviously make params1 null so you get NPE here

 params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

when you try to run a method on it (addRule(RelativeLayout.ALIGN_PARENT_RIGHT))

getLayoutParams() is for a View that has params already set. So you will need to set them for go before trying to get them to use for another View

Comments

1

This method may return null if this View is not attached to a parent ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)} was not invoked successfully. When a View is attached to a parent ViewGroup, this method must not return null.

According to the annotation of getLayoutParams() in the View.java, your ImageButton is created programly and is not attached to a parent ViewGroup when you call the method, so it returns null.

Comments

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.