简体   繁体   中英

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

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:

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM