View.setBackground抛出NoSuchMethodError

Rot*_*ens 3 android view nosuchmethoderror

以下代码:

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

产生以下错误:

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

我不知道为什么.我已经尝试过setBackground R.drawable.img但是我得到了同样的错误.

Mic*_*ael 10

此方法在API级别16中引入,您很可能在较早的版本上运行:

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

使用setBackgroundDrawable()