Error using radiobutton getText () method

0

I created a very simple app to calculate a bill with tip add 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.exercise.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>

    
asked by anonymous 24.08.2016 / 21:59

2 answers

1

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

rb = (RadioButton) findViewById(R.id.rb);
    
24.08.2016 / 22:37
1

You are trying to access a method on an object that is null (RadioButton rb).

You can resolve this or implement the calcular() method in another way.

The value returned by the rg.getCheckedRadioButtonId() method is the id of the RadioButton selected. It is the one indicated in the RadioButon declaration in xml , by the attribute android:id=""

To know which RadioButton is selected, compare the value of op with each of these id's

Change method calcular() like this:

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 still be better to replace these if's with a switch/case

    
24.08.2016 / 23:00