2014-10-20 116 views
0

我想更改FrameLayout的背景。这是我的代码使用权现在:setBackground():无法从静态上下文中引用非静态方法

FrameLayout.setBackground(getResources().getDrawable(R.drawable.background)); 

但出现此错误:

非静态方法“的setBackground(android.graphics.drawable.Drawable)” 不能从静态引用上下文。

那里有什么问题?

+0

可能重复[“非静态方法不能从静态上下文中引用”错误] (http://stackoverflow.com/questions/4922145/non-static-method-cannot-be-referenced-from-a-static-context-error) – EJP 2014-10-21 09:21:50

回答

0

FrameLayout是一类。当您拨打FrameLayout.setBackground()时,您正试图在FrameLayout类本身上调用一个名为setBackground()的静态方法。这个错误告诉你这种静态方法不存在。

你真正想要做的是在FrameLayout的特定实例上调用setBackground()方法。

首先,你需要一般通过对findViewById()一个来电索取您的FrameLayout(参考,然后调用该setBackground()

相关问题