0

I'm helping my friend developing an App and it's shutting down unexpectadly when we try to run it. I believe it's a problem with the radiobuttons functions but we are big noobs and we can't understand what could be wrong,

I would appreciate your help!

The .java code

 package com.com.calculartmb;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity {
// variaveis

double altura;
double peso;
double resultado;
double nivel;

EditText pesoEd;
EditText alturaEd;
EditText finalEd;

SeekBar altSeekBar;
SeekBar pesoSeekBar;

RadioButton radio1;
RadioButton radio2;
RadioButton radio3;
RadioButton radio4;
RadioButton radio5;
RadioButton radioGrupo;

private double[] checklistValues = new double[6];


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

    // iniciação
    pesoEd = (EditText) findViewById(R.id.editText1);
    alturaEd = (EditText) findViewById(R.id.editText2);
    finalEd = (EditText) findViewById(R.id.editText3);

    altSeekBar = (SeekBar) findViewById(R.id.seekBar1);
    pesoSeekBar = (SeekBar) findViewById(R.id.seekBar2);

    radio1 = (RadioButton) findViewById(R.id.radio1);
    radio2 = (RadioButton) findViewById(R.id.radio2);
    radio3 = (RadioButton) findViewById(R.id.radio3);
    radio4 = (RadioButton) findViewById(R.id.radio4);
    radio5 = (RadioButton) findViewById(R.id.radio5);
    radioGrupo = (RadioButton) findViewById(R.id.radioGroup1);

    altSeekBar.setMax(25000);
    pesoSeekBar.setMax(60000);


    //listeners
    addChangeListenerToRadios();
    altSeekBar.setOnSeekBarChangeListener(altSeekBarListener);
    pesoSeekBar.setOnSeekBarChangeListener(pesoSeekBarListener);

}

private OnSeekBarChangeListener pesoSeekBarListener = new OnSeekBarChangeListener() {

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        // TODO Auto-generated method stub

        // Calcula o novo valor do TIP
        peso = (pesoSeekBar.getProgress()) * .01;
        // mostra na caixa o valor novo
        pesoEd.setText(String.format("%.02f", peso).replace(',', '.'));
        // Chama o update
        updateValorTMB();

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

};

private OnSeekBarChangeListener altSeekBarListener = new OnSeekBarChangeListener() {

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        // TODO Auto-generated method stub

        // Calcula o novo valor do TIP
        altura = (altSeekBar.getProgress()) * .01;
        // mostra na caixa o valor novo
        alturaEd.setText(String.format("%.02f", altura).replace(',', '.'));
        // Chama o update
        updateValorTMB();

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

};

private void updateValorTMB() {

    double peso = Double.parseDouble(pesoEd.getText().toString());

    double altura = Double.parseDouble(alturaEd.getText().toString());

    double resultado = 655 + (9.6 * peso) + (1.8 * altura); //- (4.7 * nivel);


    finalEd.setText(String.format("%.02f", resultado));

}



public void addChangeListenerToRadios(){

    radioGrupo.setOnCheckedChangeListener(new OnCheckedChangeListener(){

        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
            // TODO Auto-generated method stub
            checklistValues[0] = (radio1.isChecked())?1.2:0;
            checklistValues[1] = (radio2.isChecked())?1.37:0;
            checklistValues[2] = (radio3.isChecked())?1.65:0;
            checklistValues[3] = (radio4.isChecked())?1.72:0;
            checklistValues[4] = (radio5.isChecked())?1.9:0;

            setNivelFromChecklist();

            updateValorTMB();

        }

    });

}

private void setNivelFromChecklist(){

    double total= 0;

    for (double item:checklistValues){

        total += item; 
    }

    nivel = total * .01;
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Here is the LogCat file

 03-06 13:02:38.340: W/dalvikvm(904): threadid=1: thread exiting with uncaught exception (group=0xb3b0fb90)
03-06 13:02:38.450: E/AndroidRuntime(904): FATAL EXCEPTION: main
03-06 13:02:38.450: E/AndroidRuntime(904): Process: com.com.calculartmb, PID: 904
03-06 13:02:38.450: E/AndroidRuntime(904): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.com.calculartmb/com.com.calculartmb.MainActivity}: java.lang.ClassCastException: android.widget.RadioGroup cannot be cast to android.widget.RadioButton
03-06 13:02:38.450: E/AndroidRuntime(904):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-06 13:02:38.450: E/AndroidRuntime(904):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-06 13:02:38.450: E/AndroidRuntime(904):  at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-06 13:02:38.450: E/AndroidRuntime(904):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-06 13:02:38.450: E/AndroidRuntime(904):  at android.os.Handler.dispatchMessage(Handler.java:102)
03-06 13:02:38.450: E/AndroidRuntime(904):  at android.os.Looper.loop(Looper.java:137)
03-06 13:02:38.450: E/AndroidRuntime(904):  at android.app.ActivityThread.main(ActivityThread.java:4998)
03-06 13:02:38.450: E/AndroidRuntime(904):  at java.lang.reflect.Method.invokeNative(Native Method)
03-06 13:02:38.450: E/AndroidRuntime(904):  at java.lang.reflect.Method.invoke(Method.java:515)
03-06 13:02:38.450: E/AndroidRuntime(904):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-06 13:02:38.450: E/AndroidRuntime(904):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-06 13:02:38.450: E/AndroidRuntime(904):  at dalvik.system.NativeStart.main(Native Method)
03-06 13:02:38.450: E/AndroidRuntime(904): Caused by: java.lang.ClassCastException: android.widget.RadioGroup cannot be cast to android.widget.RadioButton
03-06 13:02:38.450: E/AndroidRuntime(904):  at com.com.calculartmb.MainActivity.onCreate(MainActivity.java:57)
03-06 13:02:38.450: E/AndroidRuntime(904):  at android.app.Activity.performCreate(Activity.java:5243)
03-06 13:02:38.450: E/AndroidRuntime(904):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-06 13:02:38.450: E/AndroidRuntime(904):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-06 13:02:38.450: E/AndroidRuntime(904):  ... 11 more
03-06 13:03:41.418: W/dalvikvm(1082): threadid=1: thread exiting with uncaught exception (group=0xb3b0fb90)
03-06 13:03:41.448: E/AndroidRuntime(1082): FATAL EXCEPTION: main
03-06 13:03:41.448: E/AndroidRuntime(1082): Process: com.com.calculartmb, PID: 1082
03-06 13:03:41.448: E/AndroidRuntime(1082): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.com.calculartmb/com.com.calculartmb.MainActivity}: java.lang.ClassCastException: android.widget.RadioGroup cannot be cast to android.widget.RadioButton
03-06 13:03:41.448: E/AndroidRuntime(1082):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-06 13:03:41.448: E/AndroidRuntime(1082):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-06 13:03:41.448: E/AndroidRuntime(1082):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-06 13:03:41.448: E/AndroidRuntime(1082):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-06 13:03:41.448: E/AndroidRuntime(1082):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-06 13:03:41.448: E/AndroidRuntime(1082):     at android.os.Looper.loop(Looper.java:137)
03-06 13:03:41.448: E/AndroidRuntime(1082):     at android.app.ActivityThread.main(ActivityThread.java:4998)
03-06 13:03:41.448: E/AndroidRuntime(1082):     at java.lang.reflect.Method.invokeNative(Native Method)
03-06 13:03:41.448: E/AndroidRuntime(1082):     at java.lang.reflect.Method.invoke(Method.java:515)
03-06 13:03:41.448: E/AndroidRuntime(1082):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-06 13:03:41.448: E/AndroidRuntime(1082):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-06 13:03:41.448: E/AndroidRuntime(1082):     at dalvik.system.NativeStart.main(Native Method)
03-06 13:03:41.448: E/AndroidRuntime(1082): Caused by: java.lang.ClassCastException: android.widget.RadioGroup cannot be cast to android.widget.RadioButton
03-06 13:03:41.448: E/AndroidRuntime(1082):     at com.com.calculartmb.MainActivity.onCreate(MainActivity.java:57)
03-06 13:03:41.448: E/AndroidRuntime(1082):     at android.app.Activity.performCreate(Activity.java:5243)
03-06 13:03:41.448: E/AndroidRuntime(1082):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-06 13:03:41.448: E/AndroidRuntime(1082):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-06 13:03:41.448: E/AndroidRuntime(1082):     ... 11 more
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
André Ferreira
  • 185
  • 1
  • 3
  • 16
  • Did you declare `radioGrupo` as a `RadioGroup` in your XML file? – Alexis C. Mar 06 '14 at 18:14
  • You would be better off learning how to read a stack trace rather than posting it every time your software crashes. In the course of developing any program, you're going to run into errors - it's a vital skill to be able to read and understand the error messages. – Sam Dufel Mar 06 '14 at 18:16
  • Yes, we did! And there's no syntax errors and suddently crashed, that's why we didn't understood. We don't know how to read logcat's, thats why we used stackoverflow – André Ferreira Mar 06 '14 at 18:18
  • 2
    @AndréFerreira In your Java file you declared `radioGrupo` as a `RadioButton`. Declare it and cast it as a `RadioGroup`. That's what the stacktrace is saying: `java.lang.ClassCastException: android.widget.RadioGroup cannot be cast to android.widget.RadioButton` – Alexis C. Mar 06 '14 at 18:19
  • [See this answer on reading your logcat](http://stackoverflow.com/questions/16782558/what-is-the-best-way-to-debug-the-android-code-in-eclipse/16782621#16782621) – codeMagic Mar 06 '14 at 18:20
  • In your Java File you have type casted RadioGroup to RadioButton in this line radioGrupo = (RadioButton) findViewById(R.id.radioGroup1); change to radioGrupo = (RadioGroup) findViewById(R.id.radioGroup1); – Vishnu Prabhu Mar 06 '14 at 18:21

3 Answers3

2

In your logcat, there'l always be the:
"Caused by:..." line which will tell you why the error occurs followed by where it occurs.

Read the exception properly in the logcat, thats what it is for. You can search(if not actually read every line) for the line beginning with "at" and followed by your package name - that basically tells you where the error is.
In your case its pretty clear, The ClassCastException
Check your xml,

radioGrupo = (RadioButton) findViewById(R.id.radioGroup1);  

radioGrupo has to be a RadioGroup , it is declared as RadioButton, same applies for findViewById

Pararth
  • 8,114
  • 4
  • 34
  • 51
1

In your .xml file, you declared the View with the id "radioGroup1" as a RadioGroup. In your Activity, you are casting a RadioGroup to a RadioButton, which cannot work and results in a ClassCastException.

Your wrong code:

RadioButton radioGrupo;

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

    radioGrupo = (RadioButton) findViewById(R.id.radioGroup1);
}

This is how to do it correctly:

RadioGroup radioGrupo;

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

    radioGrupo = (RadioGroup) findViewById(R.id.radioGroup1);
}
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
1

Probably the problem is that:

RadioButton radioGrupo;

should be

RadioGroup radioGrupo;

And you should also edit the following assignment:

radioGrupo = (RadioButton) findViewById(R.id.radioGroup1);

to

radioGrupo = (RadioGroup) findViewById(R.id.radioGroup1);
user2340612
  • 10,053
  • 4
  • 41
  • 66