简体   繁体   English

在正确类型的Android子级上使用.setText()时,“无法解析符号”

[英]“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: 回顾一下,我有:

  • A tableRow object always containing exactly two EditTexts 一个tableRow对象始终完全包含两个EditText

and my problem is that: 我的问题是:

  • Java seems to not recognise that I am asking for .setText() on a EditText Java似乎无法识别我在EditText上要求.setText()

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. 就像将TableRowTableRowHolder投射出来一样,您需要先将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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM