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.

Community
  • 1
  • 1
Askar Zaitov
  • 283
  • 1
  • 6
  • 15
  • 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. – groff07 Aug 29 '13 at 21:54

3 Answers3

25

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.

user802421
  • 7,465
  • 5
  • 40
  • 63
Juangcg
  • 1,038
  • 9
  • 14
  • `LayoutParams.MATCH_PARENT` need to be set as `RelativeLayoutParams.MATCH_PARENT`, since there are multiple types of `LayoutParams` – anurupr Sep 11 '17 at 00:02
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

codeMagic
  • 44,549
  • 13
  • 77
  • 93
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.

canaanhhh
  • 201
  • 2
  • 7