onCreate()中的ClassCastException

时间:2014-03-06 18:13:17

标签: java android radio-button

我正在帮助我的朋友开发一个应用程序,当我们尝试运行它时,它会无意中关闭。我相信这是radiobuttons功能的一个问题,但我们是大的新手,我们无法理解可能出现的问题,

感谢您的帮助!

.java代码

 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;
}

}

这是LogCat文件

 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

3 个答案:

答案 0 :(得分:2)

在你的logcat中,总会有:
Caused by: ...”行将告诉您错误发生的原因以及发生错误的位置。

在logcat中正确读取异常,这就是它的用途。 您可以搜索(如果不是实际读取每一行)以“at”开头的行,然后是您的包名 - 这基本上可以告诉您错误的位置。
在你的情况下它非常清楚, ClassCastException 检查你的xml,

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

radioGrupo必须是RadioGroup,声明为RadioButton,同样适用于findViewById

答案 1 :(得分:1)

.xml 文件中,您将{{1>}的ID为“ radioGroup1 ”的View声明为RadioGroup。 在Activity中,您将RadioGroup投射到RadioButton,这不能正常工作并产生ClassCastException

您的错误代码:

RadioButton radioGrupo;

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

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

这是如何做到正确

RadioGroup radioGrupo;

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

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

答案 2 :(得分:1)

问题可能是:

RadioButton radioGrupo;

应该是

RadioGroup radioGrupo;

您还应编辑以下作业:

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

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