簡體   English   中英

在正確類型的Android子級上使用.setText()時,“無法解析符號”

[英]“Cannot Resolve Symbol” when .setText() used on a child of correct type Android

提前致謝。

我在一個Android應用程序中有多個TableRow對象,每個對象恰好包含兩個EditText。 我想在打開/關閉應用程序時保存和還原editText的內容和數量,因此我需要一種方法來設置EditText的文本,但這就是問題所在。

Android Studio說“無法解析符號'setText'”:

//will loop through each of the TableRows in a tableRowHolder(no problem yet):
for (int i = 0; i < tableRowHolder.getChildCount() && tableRowHolder.getChildAt(i) instanceof android.widget.TableRow; ++i) { 

    //set tableRow to be the i-th child in tableRowHolder (no problem yet)
    TableRow tableRow = (TableRow) tableRowHolder.getChildAt(i);

    //where the problem is("setText" is red), I don't think Java recognises that "tableRow.getChildAt(1)" is an EditText, even though it always will be.
    tableRow.getChildAt(1).setText();

    //this however, is perfectly fine:
    EditText et = new EditText(
    et.setText("");
}

回顧一下,我有:

  • 一個tableRow對象始終完全包含兩個EditText

我的問題是:

  • Java似乎無法識別我在EditText上要求.setText()

非常感謝。

就像將TableRowTableRowHolder投射出來一樣,您需要先將View子對象投射到EditText然后才能調用其方法。

TableRow tableRow = (TableRow) tableRowHolder.getChildAt(i);

((EditText) tableRow.getChildAt(1)).setText("Some Text");

如果有可能View不一定總是EditText ,則可以選擇將調用包裝在if塊instance內,以避免任何ClassCastException

View child = tableRow.getChildAt(1);

if (child instanceof EditText) {
    EditText et = (EditText) child;
    et.setText("Some Text");
}

暫無
暫無

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

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