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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With