gpt4 book ai didi

java - onCreate() 中的 ClassCastException

转载 作者:行者123 更新时间:2023-12-01 18:35:22 24 4
gpt4 key购买 nike

我正在帮助我的 friend 开发一个应用程序,当我们尝试运行它时,它意外关闭。我相信这是单选按钮功能的问题,但我们是大菜鸟,我们无法理解可能出了什么问题,

非常感谢您的帮助!

.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

最佳答案

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

在 logcat 中正确读取异常,这就是它的用途。您可以搜索(如果没有真正阅读每一行)以“at”开头的行,后跟您的包名称 - 这基本上可以告诉您错误所在。
就你而言,它很清楚,ClassCastException
检查你的 xml,

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

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

关于java - onCreate() 中的 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22232634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com