gpt4 book ai didi

java - ava.lang.ClassCastException : android. support.v7.widget.AppCompatTextView 无法转换为 android.widget.EditText

转载 作者:行者123 更新时间:2023-12-01 09:29:30 26 4
gpt4 key购买 nike

我对 Android 开发完全陌生,只上过 2 门类(class)。我的教授给出了一个小型项目来创建一个应用程序,该应用程序可以接收一些数字并计算膳食费用。这是我到目前为止所拥有的:

public class MainActivity extends AppCompatActivity {

/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;

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

Button cButton = (Button) findViewById(R.id.calculateButton);
cButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
//getting cost
EditText cost = (EditText)findViewById(R.id.mealCost);
String mealCost = cost.getText().toString();
int mealCostInt = Integer.parseInt(mealCost);

//getting tax percentage
EditText tax = (EditText)findViewById(R.id.tax);
String taxAmount = cost.getText().toString();
int taxAmountInt = Integer.parseInt(mealCost);

//getting tip percentage
EditText tip = (EditText)findViewById(R.id.tip);
String tipAmount = cost.getText().toString();
int tipAmountInt = Integer.parseInt(mealCost);

//calculating total amount
int totalAmount = (mealCostInt)+((taxAmountInt/100)*mealCostInt)+((tipAmountInt/100)*mealCostInt);

//displaying total amount
EditText total = (EditText)findViewById(R.id.total);
total.setText(totalAmount);
}
}

);

}


}

每次我运行代码时,应用程序都会成功加载。但是,每当我单击计算按钮时,都会收到如下错误:

--------- beginning of crash
09-18 20:25:40.169 2752-2752/com.example.shameemah.mealcalculator E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shameemah.mealcalculator, PID: 2752
java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
at com.example.shameemah.mealcalculator.MainActivity$1.onClick(MainActivity.java:35)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我搜索过类似的错误并尝试了一些我看到的解决方案,但由于我的知识有限,我不确定我到底应该做什么。我将非常感谢一些详细解释的帮助。我正在努力学习和理解事物,所以请像您在与 Android 开发经验为零的人交谈一样回答。非常感谢!

最佳答案

我认为,您在 xml 布局中的 View 之一(餐费、税费或小费)是 TextView 小部件。这就是隐式转换导致错误的原因。更改 xml 中 EditText 上的 TextView,错误一定会消失。

关于java - ava.lang.ClassCastException : android. support.v7.widget.AppCompatTextView 无法转换为 android.widget.EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39562461/

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