Logixor Logixor - 1 month ago 7
Android Question

Android If statement fails

I'm using this simple

if/else
statement to check which
RadioButton
is selected and set the text of a
TextView
regarding the selection. Well, for the first two cases (
if rb1.isChecked()
and
if rb2.isChecked()
) the code executes correctly and it just makes a calculation of two numbers. However, when the third case comes out
(!rb1.isChecked() && !rb2.isChecked())
the application crashes.
What is wrong?

if (rb1.isChecked()) {
int sum = x + y;
String result = String.valueOf(sum);
tv3.setText(result);
} else if (rb2.isChecked()) {
int rest = x - y;
String result = String.valueOf(rest);
tv3.setText(result);
} else if (!rb1.isChecked() && !rb2.isChecked()){
tv3.setText("Select an option!");
}


UPDATE:

The Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.logixor.advancedcalc">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


The MainActivity.java:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
public EditText et1,et2;
public TextView tv3;
public RadioButton rb1,rb2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

et1=(EditText)findViewById(R.id.et1);
et2=(EditText)findViewById(R.id.et2);
tv3=(TextView)findViewById(R.id.tv3);
rb1=(RadioButton)findViewById(R.id.rb1);
rb2=(RadioButton)findViewById(R.id.rb2);
}

public void calc(View view) {
String valA = et1.getText().toString();
String valB = et2.getText().toString();
int x = Integer.parseInt(valA);
int y =Integer.parseInt(valB);

if (rb1.isChecked()) {
int sum = x + y;
String result = String.valueOf(sum);
tv3.setText(result);
} else if (rb2.isChecked()) {
int rest = x - y;
String result = String.valueOf(rest);
tv3.setText(result);
}

if (!rb1.isChecked() && !rb2.isChecked()){
tv3.setText("Select an option!");
}
}
}


The activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.logixor.advancedcalc.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textNumber"
android:id="@+id/tv2"
android:layout_marginTop="27dp"
android:layout_below="@+id/et1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/et2"
android:layout_below="@+id/tv2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="@string/writeHere" />

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:padding="20dp"
android:layout_below="@+id/et2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/rg1">

<RadioButton
android:text="@string/sum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb1"
android:layout_weight="1" />

<RadioButton
android:text="@string/rest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb2"
android:layout_weight="1" />

</RadioGroup>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textNumber"
android:layout_marginTop="25dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/tv1" />

<Button
android:text="@string/calc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_alignBottom="@+id/rg1"
android:layout_marginBottom="27dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="@+id/rg1"
android:layout_toEndOf="@+id/rg1"
android:onClick="calc" />

<TextView
android:text="@string/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="19dp"
android:id="@+id/tv3"
android:textSize="24sp"
android:layout_below="@+id/rg1"
android:textAlignment="center" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_marginTop="8dp"
android:id="@+id/et1"
android:hint="@string/writeHere"
android:layout_below="@+id/tv1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

</RelativeLayout>


The strings.xml:

<resources>
<string name="app_name">AdvancedCalc</string>
<string name="textNumber">Number</string>
<string name="writeHere">Write number here...</string>
<string name="result">Result</string>
<string name="calc">CALCULATE</string>
<string name="sum">Sum</string>
<string name="rest">Rest</string>
</resources>


The FATAL ERROR from the
logcat
:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.logixor.advancedcalc, PID: 23911
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4640)
at android.view.View$PerformClick.run(View.java:19425)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4640) 
at android.view.View$PerformClick.run(View.java:19425) 
at android.os.Handler.handleCallback(Handler.java:733) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:146) 
at android.app.ActivityThread.main(ActivityThread.java:5593) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:137)
at java.lang.Integer.parseInt(Integer.java:358)
at java.lang.Integer.parseInt(Integer.java:331)
at com.logixor.advancedcalc.MainActivity.calc(MainActivity.java:30)
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:4640) 
at android.view.View$PerformClick.run(View.java:19425) 
at android.os.Handler.handleCallback(Handler.java:733) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:146) 
at android.app.ActivityThread.main(ActivityThread.java:5593) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
at dalvik.system.NativeStart.main(Native Method) 

Answer

Caused by: java.lang.NumberFormatException: Invalid int: "" means that text in EditText is empty, so you get the exception at Integer.parseInt() inside of the calc() function. Check if it's empty with TextUtils.isEmpty(). Additionally you might make use of TextUtils.isDigitsOnly().

Comments