3

The following code:

View inflate = inflater.inflate(R.layout.page, null);
Drawable img = getResources().getDrawable((Integer) (item.get("img")));
inflate.findViewById(R.id.page_img).setBackground(img);

produces the following error:

java.lang.NoSuchMethodError: android.view.View.setBackground

I have no idea why. I already tried the setBackground with R.drawable.img but I get the same error.

Rothens
  • 755
  • 7
  • 18

3 Answers3

10

This method was introduced in API level 16, you are most probably running on an earlier one:

http://developer.android.com/reference/android/view/View.html#setBackground(android.graphics.drawable.Drawable)

Use setBackgroundDrawable()

Michael
  • 1,416
  • 1
  • 9
  • 21
2

setBackground method was added in API 16 so if you are testing it on lower than that it will give NoSuchMethodError

Check this link for api level details

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Apoorv
  • 13,470
  • 4
  • 27
  • 33
1

yeah, please read the api: http://developer.android.com/reference/android/view/View.html#setBackgroundResource(int)

and you should know setBackground works when level 16, so you should always think about one thing, that is should i use a level 1 method instead......

hope this is helpful to you.

Acelasi Eu
  • 914
  • 2
  • 9
  • 30
Wade
  • 71
  • 1
  • 4