Error: java.lang.NumberFormatException
Error in file: activity_main_user.xml
Elements related: one EditText(PlayerMoneyToUpdate) field, and TextView(PlayerMoneyValue)
The structure of the project:
There are two Activities:
MainActivity.java is the main UI of app and contains a layout displaying all the users i.e, players of the game
MainUserActivity.java is the UI which displays the selected player and information related to it such as player name, Money(if exists) and etc.
There are also two layout XML files for this:
activity_main.xml, again, contains the main UI, no problems related to it
activity_main_user.xml, where the error is occurring.
this is the activity_main_user.xml code which has an EditText field:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/UserAvatar"
android:layout_width="160dp"
android:layout_height="149dp"
android:contentDescription="@string/AvatarDescription"
app:srcCompat="@drawable/monopolycloud" />
<View
android:id="@+id/divider8"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="?android:attr/listDivider" />
<TextView
android:id="@+id/PlayerId"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="@string/PlayerIdText"
android:textSize="30sp" />
<View
android:id="@+id/divider9"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider" />
<TextView
android:id="@+id/MoneyValue"
android:layout_width="match_parent"
android:layout_height="60dp"
android:textSize="30sp"
android:text="@string/DefaultMoney"/>
<EditText
android:id="@+id/EditTextField"
android:layout_width="match_parent"
android:layout_height="80dp"
android:autofillHints="true"
android:hint="@string/EditTextFieldText"
android:inputType="number" />
<Button
android:id="@+id/AddButton"
android:layout_width="match_parent"
android:layout_height="65dp"
android:text="@string/AddButtonText" />
<View
android:id="@+id/divider6"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="?android:attr/listDivider" />
<Button
android:id="@+id/SubtractButton"
android:layout_width="match_parent"
android:layout_height="65dp"
android:text="@string/SubtractButtonText" />
<View
android:id="@+id/divider7"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="?android:attr/listDivider" />
<Button
android:id="@+id/GoToMainMenu"
android:layout_width="match_parent"
android:layout_height="65dp"
android:text="@string/GoToMainMenu" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
in the activity_main_user.java file, I have defined a function where, upon starting of this file, the playerid and money fields get updated. upon adding a value in the EditText and pressing AddMoney button, it throws an exception java.lang.NumberFormatException for string ""
The main block of code related :
AddButton.setOnClickListener(v -> {
try {
mValue1 = Integer.parseInt(NewMoneyValue.getText().toString());
Log.d("Monopoly Cashier value1", String.valueOf(mValue1));
mValue2 = Integer.parseInt(MoneyValue.getText().toString());
Log.d("Monopoly Cashier value2", String.valueOf(Integer.parseInt(MoneyValue.getText().toString())));
mNewMoney = add(mValue1, mValue2);
MoneyValue.setText("Current Money: $" + mNewMoney);
MoneyAddedToast.show();
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
Log.d("Monopoly CashierError: ", e.getMessage());
}
});
Please help me I getting really frustrated over this. Thanks in advance.
P.S: I am a beginner to Android programming in java, so please try to be as simple as possible. P.S 2: I know that there are many questions related to my problem but I couldn't understand any of them as they weren't relatable to what I am facing