0
I created a very simple app to calculate an account with extra tip where I enter a value, select a Radio Button with a percentage and it generates the final value as a result.
But every time I click on "Calculate" it stops working and closes with the error:
FATAL EXCEPTION: main Process: com.exercicios.tipcalc, PID: 1870 java.lang.Illegalstateexception: Could not execute method for android:onClick>
......
Follow the code below:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText input;
private TextView output;
private RadioGroup rg;
private RadioButton rb;
private Button calcular;
private int op = 0;
private double valor = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.input);
output = (TextView) findViewById(R.id.output);
rg = (RadioGroup) findViewById(R.id.rg);
calcular = (Button) findViewById(R.id.calcular);
}
public void calcular(View view){
op = rg.getCheckedRadioButtonId();
valor = Double.parseDouble(input.getText().toString());
//output.setText(input.getText());
if(rb.getText().equals("10%")){
output.setText(String.valueOf(valor+(valor*.10)));
}else if(rb.getText().equals("15%")){
output.setText(String.valueOf(valor+(valor*.15)));
}else if(rb.getText().equals("20%")){
output.setText(String.valueOf(valor+(valor*.20)));
}
}
}
This is the XML of my screen:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/output"
android:text="carambola"
android:layout_below="@+id/calcular"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/input"
android:layout_below="@+id/titulo"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calcular"
android:id="@+id/calcular"
android:layout_below="@+id/rg"
android:layout_centerHorizontal="true"
android:onClick="calcular" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Informe o valor da conta"
android:id="@+id/titulo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Escolha a porcentagem da gorjeta"
android:id="@+id/titulo2"
android:layout_below="@+id/input"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rg"
android:layout_marginTop="31dp"
android:layout_below="@+id/titulo2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10%"
android:id="@+id/val1"
android:layout_below="@+id/titulo2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15%"
android:id="@+id/val2"
android:layout_below="@+id/val1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20%"
android:id="@+id/val3"
android:layout_below="@+id/val2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:onClick="calcular" />
</RadioGroup>
You are right, I entered it as follows: Rb = (Radiobutton) findViewById(op); E worked. Thank you!
– leoSandrini
@overRider This is not the most "correct" way to do.
– ramaral
@I’m sorry, but I’m just following the logic he started. The "right' way of doing" is not the case here, but help to resolve the situation in order to continue development. I did my part! Abs
– viana
See that my comment starts with @overRider, I refer to what he says in the comment and not to his reply.
– ramaral
I get @ramaral cool the way you did. Hugs.
– viana