the_prole
the_prole

Reputation: 8945

Setting drawable to layout background results in null object reference error

I am trying to convert an URI into a bit map into a drawable:

Bitmap bitmap = BitmapFactory.decodeFile(List.get(0).imageURI); 
Drawable drawable = new BitmapDrawable(getResources(), bitmap); 

I then set the drawable as the background of a layout:

layoutObject.setBackground(drawable); 

However, setting the background to the drawable yields this error:

Attempt to invoke virtual method
'void android.widget.LinearLayout.
setBackgroundDrawable(android.graphics.drawable.Drawable)'
on a null object reference

Upvotes: 0

Views: 1517

Answers (1)

Sharjeel
Sharjeel

Reputation: 15798

either layoutObject isn't defined or drawable has null value. First check if either of them is not null before setting background.

Upvotes: 2

Related Questions