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

ava.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText

我对 android 开发完全陌生,我只参加了 2 类。我的教授作为一个小型项目给出了创建一个应用程序,该应用程序可以输入一些数字并计算餐费。这是我目前所拥有的:

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.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 布局中的视图之一(餐费、税费或小费)是 TextView 小部件。这就是隐式转换导致错误的原因。在 xml 中更改 EditText 上的 TextView,错误必须消失。