繁体   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