java Android Studio getText() 和 setText() 无法处理错误:“无法解析方法“getText/setText”()”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38489679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:26:56  来源:igfitidea点击:

Android Studio getText() and setText() not working with error: "can't resolve method "getText/setText"()"

javaandroidandroid-studio

提问by Danny Piper

So my app needs to read text from a text box with a tag of "inText" do stuff to it (that stuff works) then write the output to a box with the id of "outView". I've been doing this with setText() and getText().

因此,我的应用程序需要从带有“inText”标签的文本框中读取文本对其进行处理(该操作有效),然后将输出写入 ID 为“outView”的框中。我一直在用 setText() 和 getText() 来做这件事。

setText() was for writing the output below is what I used:

setText() 用于编写下面的输出是我使用的:

(TextView)findViewById(R.id.outView.setText(textoutput));

getText() was for reading the input text then writing it to a variable and below is what I used:

getText() 用于读取输入文本,然后将其写入变量,下面是我使用的:

String mEdit = (EditText)findViewById(R.id.inText.getText()).toString();

回答by Vucko

You're chaining the method at the wrong spot. Remove .getText()from R.id.inTextand place it after the brackets like (same thing for TextView):

您将方法链接到错误的位置。.getText()R.id.inText括号中删除并将其放在括号之后,例如(与 相同TextView):

String mEdit = ((EditText)findViewById(R.id.inText)).getText().toString();

Though this is an uncommon way to do thing. Rather initialize the EditText first and then get the text, it's much clearer that way:

虽然这是一种不常见的做事方式。而是先初始化 EditText 然后获取文本,这样更清晰:

EditText mEdit = (EditText)findViewById(R.id.inText);
String mText = mEdit.getText().toString();

回答by Md Sufi Khan

You set the getText()and setText()method in wrong place. getText()and setText()are methods of TextView and EditText classes. But here you used it as a method of id. That's why it's showing "Can't resolve method getText/setText()". As id has no such methods.

您将getText()andsetText()方法设置在错误的位置。 getText()setText()是 TextView 和 EditText 类的方法。但是在这里您将其用作 id 的方法。这就是为什么它显示“无法解析方法 getText/setText()”的原因。因为 id 没有这样的方法。

You can do the following.

您可以执行以下操作。

((TextView) findViewById(R.id.outView)).setText(textoutput);

and

String mEdit = ((EditText) findViewById(R.id.inText)).getText().toString();

回答by Ido Naveh

It's not working since you need to first set the TextView:

它不起作用,因为您需要先设置 TextView:

 TextView tvOut = (TextView) findViewById(R.id.outView);
 TextView tvIn = (TextView) findViewById(R.id.inText);

 String out = tvOut.getText().toString();
 String in = tvIn.setText(out);

回答by sivi

If you use a field check the compiler didn't automatically set it as a view instead of EditText type.

如果您使用字段检查,编译器不会自动将其设置为视图而不是 EditText 类型。

Also the casts are redundant now so you better not use them if you don't have to

此外,演员阵容现在是多余的,因此如果您不必,最好不要使用它们