简体   繁体   English

我无法在 Android 应用程序的 TextView 中设置字符串(使用 Java)

[英]I can't set a string in a TextView in an Android app (using Java)

I'm building a simple calculator Android app in Java that will receive 2 numbers as inputs and when the user presses one of the 4 action buttons (+, -, *, /) the exercise and it's solution will appear in the bottom of the screen inside a TextView in this format:我正在用 Java 构建一个简单的计算器 Android 应用程序,它将接收 2 个数字作为输入,当用户按下 4 个操作按钮(+、-、*、/)之一时,练习及其解决方案将出现在底部此格式的 TextView 中的屏幕:

{num1} {action} {num2} = {solution} {num1} {action} {num2} = {solution}

I tried to declare a string and form the exercise's string in it and in the end I used "setText" to change the TextView but instead of showing the full exercise when I run the app it shows something like "androidx.appcompat.widget.AppCom".我尝试声明一个字符串并在其中形成练习的字符串,最后我使用“setText”来更改 TextView,但在运行应用程序时没有显示完整的练习,而是显示类似“androidx.appcompat.widget.AppCom”的内容”。

Here is an example for the string I form when the user clicks on the + button:这是我在用户单击 + 按钮时形成的字符串的示例:

exerciseStr = etNum1.toString() + " + " + etNum2.toString() + " = " + String.valueOf(Integer.valueOf(etNum1.getText().toString())+Integer.valueOf(etNum2.getText() + ""));练习Str = etNum1.toString() + " + " + etNum2.toString() + " = " + String.valueOf(Integer.valueOf(etNum1.getText().toString())+Integer.valueOf(etNum2.getText() + ""));

Does anybody know what the issue may be?有人知道可能是什么问题吗?

您应该在调用toString()之前调用getText() toString()

exerciseStr = etNum1.getText().toString() + " + " + etNum2.getText().toString() + " = " + String.valueOf(Integer.valueOf(etNum1.getText().toString())+Integer.valueOf(etNum2.getText() + ""));

改成这样。

exerciseStr = etNum1.getText().toString() + " + " + etNum2.getText().toString() + " = "  + String.valueOf(Integer.valueOf(etNum1.getText().toString())+Integer.valueOf(etNum2.getText() + ""));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 TextView 的文本设置为字符串资源? (安卓的Java) - How do I set text of TextView to a string resource? (Java for android) 如何使用Java代码设置TextView的位置? - How can I set the position of a TextView using Java Code? 在向 TextView 添加一些单词后,我尝试使用一个简单的按钮更改 java android 中字符串的某些部分,但它不起作用 - i try to change some parts of the String in java android using a simple button after i add some words to the TextView, but it didn't work Android / Java:使用字符串索引的char设置textview - Android/Java: Set textview with char from index of string Android:SQLite中的字符串不能在Textview中设置为HTML - Android: String from SQLite can not be set as HTML in Textview 如何在Java类中获取TextView值作为字符串并进行设置? - How do I get the TextView value as string in java class and set it? Android Wear NullPointerException无法在TextView上设置文本 - Android Wear NullPointerException can't set text on TextView 如何从 Java 设置 Android TextView 的文本? - How do I set the text of an Android TextView from Java? 我无法在 Android Studio 中为我的应用设置图标 - I can't set an icon for my app in Android Studio Android 应用程序:如何用 java 代码打印出一个字符串? - Android App: How can I print out a String with java code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM