簡體   English   中英

setBackground():不能從靜態上下文中引用非靜態方法

[英]setBackground(): non-static method cannot be referenced from a static context

我想更改 FrameLayout 的背景。 這是我現在使用的代碼:

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

但是出現這個錯誤:

不能從靜態上下文中引用非靜態方法“setBackground(android.graphics.drawable.Drawable)”。

有什么問題嗎?

FrameLayout是一個類。 當您調用FrameLayout.setBackground()您試圖在 FrameLayout 類本身上調用一個名為setBackground()的靜態方法。 這個錯誤告訴你這樣的靜態方法不存在。

您真正想要做的是在 FrameLayout 的特定實例上調用setBackground()方法。

首先,您需要獲取對 FrameLayout 的引用(通常通過調用findViewById() ,然后調用setBackground()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM