Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageView getLayoutParams() returns null

Tags:

I'm creating a widget programicaly with this simple method

private ImageView getModuleIconView(Module module) {     ImageView view = new ImageView(context);     view.setImageResource(module.getIconResId());             view.getLayoutParams().height = 50; //NullPointerException     return view; } 

I get the NullPointer Exception because getLayoutParams returns null. Why is that?

like image 714
Jacek Kwiecień Avatar asked Oct 25 '12 20:10

Jacek Kwiecień


1 Answers

Because your imageview is on air, you didn't put it on screen. So it has not have any layout params or any size.

If you want to set image's height to 50 you may consider to set layoutParams of this view... You can set layoutParams' height as 50. So it'll do what you seek.

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 50));  img.setLayoutParams(lp); 

these lines will set your image's height to 50, but don't forget i'm assuming that your parent view as linear layout, if it's not, then you should use its layoutparams.

like image 149
yahya Avatar answered Sep 17 '22 08:09

yahya