[英]“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.