Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null Pointer Exception when setting LayoutParams

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.

like image 425
Askar Zaitov Avatar asked Aug 29 '13 21:08

Askar Zaitov


3 Answers

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.

like image 70
Juangcg Avatar answered Sep 22 '22 11:09

Juangcg


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

like image 36
codeMagic Avatar answered Sep 21 '22 11:09

codeMagic


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.

like image 33
canaanhhh Avatar answered Sep 24 '22 11:09

canaanhhh