[英]“Cannot Resolve Symbol” when .setText() used on a child of correct type Android
thanks in advance. 提前致谢。
I have multiple TableRow objects in an Android app, each of which contains exactly two EditTexts. 我在一个Android应用程序中有多个TableRow对象,每个对象恰好包含两个EditText。 I want to have the contents and quantity of editTexts saved and restored when I open/close the app and so I need to have a way to set the text of the EditTexts, but this is where the problem is. 我想在打开/关闭应用程序时保存和还原editText的内容和数量,因此我需要一种方法来设置EditText的文本,但这就是问题所在。
Android Studio is saying "Cannot Resolve Symbol 'setText'": 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("");
}
To recap, I have: 回顾一下,我有:
and my problem is that: 我的问题是:
Thanks so much in advance. 非常感谢。
Just like you're casting your TableRow
out of the TableRowHolder
, you need to cast the View
child to an EditText
before you can call its methods. 就像将TableRow
从TableRowHolder
投射出来一样,您需要先将View
子对象投射到EditText
然后才能调用其方法。
TableRow tableRow = (TableRow) tableRowHolder.getChildAt(i);
((EditText) tableRow.getChildAt(1)).setText("Some Text");
You could optionally wrap your calls inside an instanceof if-block to avoid any ClassCastException
s if there's any chance that the View
may not be an EditText
always. 如果有可能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.