0

I'm getting the following Error:

java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText at com.example.bmicalculator.MainActivity.initializeApp(MainActivity.java.32)

Main Activity:

public class MainActivity extends Activity {

private EditText heightIn;
private EditText weightIn;
private Button CalculateButton;
private double weight =0;
private double height =0;
private TextView BmiResult;

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

    private void initializeApp() {
        heightIn = (EditText) findViewById (R.id.HeightInput1);
        weightIn = (EditText) findViewById (R.id.WeightInput1);
        CalculateButton = (Button) findViewById (R.id.CalculateButton);
        BmiResult = (TextView) findViewById (R.id.BmiResult);

    }

    public void calculateBMI(View v) {
        weight = Double.parseDouble(weightIn.getText().toString());
        height = Double.parseDouble(heightIn.getText().toString());
        double bmi = (weight / (height * height));
        String result = String.format("%.2f", bmi);
        Log.d("MyActivity", result);
        BmiResult.setText(result, TextView.BufferType.NORMAL);
    }
}

XML File:

<LinearLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >

<TextView
    android:id="@+id/HeightInput1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/HeightInput"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/InchesHint"
    android:inputType="numberDecimal" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/WeightInput1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/WeightInput"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/PoundsHint"
    android:inputType="numberDecimal" />

<Button
    android:id="@+id/CalculateButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="calculateBMI"
    android:text="@string/CalculateButton" />

<TextView
    android:id="@+id/BmiResult"
    android:layout_width="100dp"
    android:layout_height="0dp"
    android:layout_weight="0.00"
    android:text="@string/BmiResult" />

6
  • Would really appreciate anybody's help! Commented Oct 30, 2014 at 11:43
  • Change your editTexts id as HeightInput1 and WeightInput1 Commented Oct 30, 2014 at 11:45
  • Thank you so much, that stopped the program from crashing. Would you have any idea why my calculation is not working though? Commented Oct 30, 2014 at 11:51
  • You have to implement ButtonClickListener to perform your operation when u click on 'CalculateButton' Commented Oct 30, 2014 at 11:54
  • CalculateButton = (Button) findViewById (R.id.CalculateButton); CalculateButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { calculateBMI(); } }); } public void calculateBMI() { weight = Double.parseDouble(weightIn.getText().toString()); height = Double.parseDouble(heightIn.getText().toString()); double bmi = (weight / (height * height)); String result = String.format("%.2f", bmi); Log.d("MyActivity", result); BmiResult.setText(result, TextView.BufferType.NORMAL); Commented Oct 30, 2014 at 12:29

6 Answers 6

4

Your heightIn is not EditText and you're trying to cast it as EditText so you got Error

java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText at com.example.bmicalculator.MainActivity.initializeApp(MainActivity.java.32)

so do as,

heightIn = (TextView) findViewById (R.id.HeightInput1);
weightIn = (TextView) findViewById (R.id.WeightInput1);
Sign up to request clarification or add additional context in comments.

Comments

2

Change this:

<TextView
    android:id="@+id/HeightInput1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/HeightInput"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/WeightInput1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/WeightInput"
    android:textAppearance="?android:attr/textAppearanceMedium" />

Via

<EditText
    android:id="@+id/HeightInput1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/HeightInput"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/WeightInput1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/WeightInput"
    android:textAppearance="?android:attr/textAppearanceMedium" />

OR

heightIn = (EditText) findViewById (R.id.HeightInput1);
weightIn = (EditText) findViewById (R.id.WeightInput1);

via

heightIn = (TextView) findViewById (R.id.HeightInput1);
weightIn = (TextView) findViewById (R.id.WeightInput1);

1 Comment

Hope this may help you!
2

in java file

heightIn = (EditText) findViewById (R.id.HeightInput1);

and

in xml file

<TextView
    android:id="@+id/HeightInput1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/HeightInput"
    android:textAppearance="?android:attr/textAppearanceMedium" />

So change according to your requirement.

Comments

1

In Xml file you are declare as TextView and you are trying to parse TextView to EditText that's why you are getting an Exception.

Change EditText Variable

private EditText heightIn;
private EditText weightIn;

To

private TextView heightIn;
private TextView weightIn;

And replace your initializeApp() function with:

private void initializeApp() {
    heightIn = (TextView) findViewById (R.id.HeightInput1);
    weightIn = (TextView) findViewById (R.id.WeightInput1);
    CalculateButton = (Button) findViewById (R.id.CalculateButton);
    BmiResult = (EditText) findViewById (R.id.BmiResult);

}

Comments

1
 heightIn = (EditText) findViewById (R.id.HeightInput1);

Here you mentioned heightIn as EditTect and in your layout.xml it is as TextView,
instead of finding "HeightInput1" which is just your text to label, it should find the EditText

 heightIn = (EditText) findViewById (R.id.EditText1);   

same with the weight input... good luck!

Comments

1
  1. There are two possibilities, HeightInput1 id component should be Edit text not a text view
  2. else In code change the Edit text to Text view whose id is HeightInput1

so change java code with this

    heightIn = (TextView) findViewById (R.id.HeightInput1);  
    weightIn = (TextView) findViewById (R.id.WeightInput1);

else change in xml file with this

  <EditText
   android:id="@+id/HeightInput1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/HeightInput"
   android:textAppearance="?android:attr/textAppearanceMedium" />

3 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
is it ok now ,else do you think anything more can be done to this?
Yeah, that does look like a nice answer. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.