2012-09-22 69 views
2

我得到一個錯誤,當我打電話的onCreate()如何在活動的OnCreate()方法中調用findViewWithTag?

findViewWithTag()「的方法findViewWithTag(String)是未定義類型 MainActivity」

有什麼選擇?

+0

它只有findViewWithTag,對不對? – Enigman

+0

該方法是['View#findViewWithTag()'](http://developer.android.com/reference/android/view/View.html#findViewWithTag%28java.lang.Object%29),它不是Activity類。也許你想使用更有用的'findViewById()'方法? – Sam

+0

@Sam no,我真的需要使用findViewWithTag,因爲我必須遍歷許多TextView並設置它們的文本。因此,我已經爲它們中的每一個添加了標籤,從01,02 ...直到26,27。 由於它是一個字符串,因此我訪問標籤比較容易。 有一些只使用他們的ID的出路嗎? – Enigman

回答

6

findViewWithTag()僅針對View定義。

如果您有使用setContentView(contentView)設置的活動的contentView參考,則可以使用contentView.findViewWithTag()

如果您沒有對該視圖的引用,則還可以使用findViewById(R.id.your_content_view_id).findViewWithTag(tag)

+2

半正確,方法確實是['findViewWithTag()'](http://developer.android.com/reference/android/view/View.html#findViewWithTag%28java.lang.Object%29),但它是View的一部分而不是Activity類。 – Sam

+0

@Sam,是的,那就是我剛纔提到的。我的回答有錯嗎? –

+0

您正在使用'findViewByTag()',而不是'findViewWithTag()'... – Sam

相關問題