ava.lang。android.support.v7.widget ClassCastException。不能将AppCompatTextView强制转换为android.widget.EditText

时间:2021-05-12 15:52:08

I am completely new to android development and I have taken only 2 classes. My professor has given as a mini project to create an app that takes in some numbers and calculates the meal cost. This is what i have so far:

我对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);
                }
            }

        );

    }


}

Every time I run the code, the app loads successfully. However, whenever I click on the calculate button, I get the error seen below:

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

--------- 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)

I have searched for similar errors and tried some solutions I saw but because of my limited knowledge I am not sure what exactly I should be doing. I will truly appreciate some help with some detailed explanations. I am trying hard to learn and understand things so please answer like you're talking to someone who has zero experience with android dev. Thank you so much!

我搜索过类似的错误,并尝试过我看到的一些解决方案,但由于我的知识有限,我不确定我应该做什么。我真的很感激你能给我一些详细的解释。我正在努力学习和理解一些东西,所以请回答,就像你在和一个没有android开发经验的人交谈一样。

1 个解决方案

#1


1  

I think, one of your views (mealCost, tax or tips) in xml layout is TextView widget. That's why implicitly converting causes the error. Change TextView on EditText in xml and the error must disappear.

我认为,在xml布局中,您的一个视图(mealCost、tax或tips)是TextView小部件。这就是为什么隐式转换会导致错误。在xml中修改EditText上的TextView,错误必须消失。

#1


1  

I think, one of your views (mealCost, tax or tips) in xml layout is TextView widget. That's why implicitly converting causes the error. Change TextView on EditText in xml and the error must disappear.

我认为,在xml布局中,您的一个视图(mealCost、tax或tips)是TextView小部件。这就是为什么隐式转换会导致错误。在xml中修改EditText上的TextView,错误必须消失。