应用程序崩溃,不确定错误指向哪里。

huangapple go评论81阅读模式
英文:

App crash, not sure what the error is pointing me too

问题

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.homework1, PID: 7027
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.homework1/com.example.homework1.MainActivity}: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.EditText
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.EditText
    at com.example.homework1.MainActivity.onCreate(MainActivity.java:32)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
英文:

Whenever I run this program in Andriod studio The app just cashes on the phone.

package com.example.homework1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
double tipPercent;
SeekBar seekBar;
TextView tipAmountString;
double tipAmountCalc;
TextView totals;
EditText BaseBillTotal;
EditText TipValue;
double baseValueCalc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TipValue = findViewById(R.id.TipValue);
seekBar = findViewById(R.id.seekBar);
tipAmountString = findViewById(R.id.tipAmountString);
totals = findViewById(R.id.TotalValueScreen);
BaseBillTotal = findViewById(R.id.BillBaseValue);
baseValueCalc = Double.parseDouble(BaseBillTotal.getText().toString());
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tipAmountString.setText(String.valueOf(progress));
tipAmountCalc = progress*.10;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
RadioGroup radioGroup = findViewById(R.id.tipPercentGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.radioButton){
tipPercent = .10;
}
else if (checkedId == R.id.radioButton2){
tipPercent = .15;
}
else if (checkedId == R.id.radioButton3){
tipPercent = .18;
}
else if (checkedId == R.id.radioButton4)
{
tipPercent = tipAmountCalc;
}
}
});
TipValue.setText(String.valueOf(tipPercent*100));
totals.setText(String.valueOf(tipPercent * baseValueCalc) );
}
}

This is the error in the console
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.homework1, PID: 7027
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.homework1/com.example.homework1.MainActivity}: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.EditText
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.EditText
at com.example.homework1.MainActivity.onCreate(MainActivity.java:32)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

答案1

得分: 1

堆栈跟踪说明了什么出错以及原因:

java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout无法转换为android.widget.EditText,位于com.example.homework1.MainActivity.onCreate(MainActivity.java:32)

你的MainActivity的第32行是BaseBillTotal = findViewById(R.id.BillBaseValue);

你在Activity中将BaseBillTotal定义为EditText,但根据这个异常,你的布局文件中有一个ID为BillBaseValueConstraintLayout,而你无法将其转换为EditText。请仔细检查你的布局文件,并验证类型和ID。

英文:

The stack trace explains exactly what is breaking and why:


java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.EditText at com.example.homework1.MainActivity.onCreate(MainActivity.java:32)

line 32 of your MainActivity is BaseBillTotal = findViewById(R.id.BillBaseValue);

You have BaseBillTotal defined in your Activity as an EditText, but according to this exception, your layout file has a ConstraintLayout with an id of BillBaseValue, and you can't cast that to an EditText. Double check your layout file and verify the type and ID there.

答案2

得分: 0

请检查您的编辑文本的id。根据错误提示,您可能在XML布局中给约束布局设置了BillBaseValueTipValue的id,而不是实际编辑文本的id。

英文:

Check the id of your edit texts. According to the error you might have given the constraint layout the id of BillBaseValue or TipValue instead of the actual edit text in your xml layout.

huangapple
  • 本文由 发表于 2020年9月20日 11:33:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63975257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定