Error using Radiobutton gettext() method

Asked

Viewed 177 times

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>

2 answers

1


You’re calling a rb.getText() without instantiating. You should add there in your MainActivity:

rb = (RadioButton) findViewById(R.id.rb);
  • You are right, I entered it as follows: Rb = (Radiobutton) findViewById(op); E worked. Thank you!

  • @overRider This is not the most "correct" way to do.

  • @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

  • See that my comment starts with @overRider, I refer to what he says in the comment and not to his reply.

  • 1

    I get @ramaral cool the way you did. Hugs.

1

You are trying to access a method on a null object (Radiobutton Rb).

You can solve this or implement the method calcular() otherwise.

The value returned by the method rg.getCheckedRadioButtonId() is the id of Radiobutton selected. This is the one specified in the Radiobuton in the xml, by attribute android:id=""

To know what the Radiobutton selected, compare the value of op with each of these id's

Change the method calcular() thus:

public void calcular(View view){
    op = rg.getCheckedRadioButtonId();
    valor = Double.parseDouble(input.getText().toString());
    //output.setText(input.getText());
    if(op == R.id.val1){
        output.setText(String.valueOf(valor+(valor*.10)));
    }else if(op == R.id.val2){
        output.setText(String.valueOf(valor+(valor*.15)));
    }else if(op == R.id.val3){
        output.setText(String.valueOf(valor+(valor*.20)));
    }
}

It would be even better to replace these if'sby a switch/case

Browser other questions tagged

You are not signed in. Login or sign up in order to post.