[英]“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
從TableRowHolder
投射出來一樣,您需要先將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.