I’m pretty new to android so I will try to explain this the best way I can,
so I have a EditView in an activity I created like this:
<EditText
android:id="@+id/num"
android:layout_width="230dp"
android:layout_height="61dp"
android:inputType="number"
android:background="@drawable/border"
android:layout_marginTop="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.129" />
and in my MainActivity.kt I save the number user input’s like this:
val number = findViewById<EditText>(R.id.num)
but when I try to sum the number user input’s with a number I declared in my code like this:
var summ = 1
summ += number
When I launch program I says this:
None of the following functions can be called with the arguments supplied: public final operator fun plus(other: Byte): Int defined in kotlin.Int public final operator fun plus(other: Double): Double defined in kotlin.Int public final operator fun plus(other: Float): Float defined in kotlin.Int public final operator fun plus(other: Int): Int defined in kotlin.Ints public final operator fun plus(other: Long): Long defined in kotlin.Int public final operator fun plus(other: Short): Int defined in kotlin.Int
If anyone knows how I could save the number user input’s with a number I save in my MainActivity.kt please let me know!
3
Answers
Try this:
Using this should work:
Since
findViewById<EditText>(R.id.num)
returns a View not an Int.summ
is anInt
object, which represents an integer.number
is anEditText
object, which is a UI widget that (among other things) displays text and allows the user to change the contents of that text. I know that might sound pedantic, but the point is those are two completely different things and you can’t just "add" them to each other, it’s like saying2 + 🍌
.The other answers tell you how to get the
String
contents of theEditText
(which you also can’t add to a number), and how to convert aString
to anInteger
(now you can add them – if the conversion worked!) but this is a general concept you need to understand – it won’t always be as obvious that two different types can’t just be combined, and you’ll have to find a way to get what you need.someTextView.text
for example is not aString
, and you need to calltoString()
on it if you want the basic text contents