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

标签 java android debugging classcastexception

我对 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/

相关文章:

java - 如何在CSS中设置背景图片?

java - 关闭扫描器会抛出 java.util.NoSuchElementException

android - 选项卡不显示

javascript - 在页面重新加载时, "window.print()"不会在 Google Chrome 77.0.3865.120 中加载打印对话框。当 IFRAME 与作为 SRC 的 PDF 一起使用时会发生这种情况

swift - 如何在 Xcode 中使用 LLDB 获取附加到 View 的所有手势识别器?

java - 字节数组到十六进制字符数组的转换

java - 在构建/部署时更改配置文件

c++ - 如何检查 Linux 上 C++ Qt 应用程序中的内存泄漏?

android - 为什么这些依赖项在 build.gradle 中重新排列? : Android Studio

php - $_POST 值在 SSL 打开后消失