![](/img/trans.png)
[英]Android java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText
[英]java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
我收到以下錯誤:
java.lang.ClassCastException:android.widget.TextView無法轉換為com.example.bmicalculator.MainActivity.initializeApp(MainActivity.java.32)上的android.widget.EditText
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);
}
}
<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" />
您的heightIn
不是EditText
並且您嘗試將其heightIn
為EditText
因此出現錯誤
java.lang.ClassCastException:android.widget.TextView無法轉換為com.example.bmicalculator.MainActivity.initializeApp(MainActivity.java.32)上的android.widget.EditText
這樣做
heightIn = (TextView) findViewById (R.id.HeightInput1);
weightIn = (TextView) findViewById (R.id.WeightInput1);
更改此:
<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" />
通過
<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" />
要么
heightIn = (EditText) findViewById (R.id.HeightInput1);
weightIn = (EditText) findViewById (R.id.WeightInput1);
通過
heightIn = (TextView) findViewById (R.id.HeightInput1);
weightIn = (TextView) findViewById (R.id.WeightInput1);
在java文件中
heightIn = (EditText) findViewById (R.id.HeightInput1);
和
在xml文件中
<TextView
android:id="@+id/HeightInput1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/HeightInput"
android:textAppearance="?android:attr/textAppearanceMedium" />
因此,請根據您的要求進行更改。
所以用這個改變java代碼
heightIn = (TextView) findViewById (R.id.HeightInput1);
weightIn = (TextView) findViewById (R.id.WeightInput1);
否則在xml文件中更改
<EditText
android:id="@+id/HeightInput1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/HeightInput"
android:textAppearance="?android:attr/textAppearanceMedium" />
在Xml文件中,您將聲明為TextView,並且嘗試將TextView解析為EditText,這就是為什么您獲得異常的原因。
更改EditText變量
private EditText heightIn;
private EditText weightIn;
至
private TextView heightIn;
private TextView weightIn;
並將您的initializeApp()函數替換為:
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);
}
heightIn = (EditText) findViewById (R.id.HeightInput1);
在這里,您將heightIn稱為EditTect,在layout.xml中將其稱為TextView,
而不是找到只是要標記的文本“ HeightInput1”,它應該找到EditText
heightIn = (EditText) findViewById (R.id.EditText1);
與重量輸入相同...祝您好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.