这是我的布局文件。当我尝试在我的模拟器上运行这个Android应用程序它崩溃并在logcat中我得到logcat错误:
classcastexception: android.Widget.EditText cannot be cast to android.View.ViewGroup.
我在谷歌搜索此错误,发现当视图未正确关闭时会发生这种情况。但我检查并没有发现任何错误。任何帮助和建议表示赞赏。谢谢。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.form_app.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="44dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text=" Information form"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="24dp"
android:text="NAME"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/name"
android:layout_marginTop="33dp"
android:text="AGE"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/submit"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView5"
android:layout_marginTop="28dp"
android:text="SUBMIT" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_marginRight="18dp"
android:ems="10"
android:inputType="textPersonName" />
<EditText
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/name"
android:layout_alignTop="@+id/textView3"
android:ems="10"
android:inputType="number" />
<Spinner
android:id="@+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView4"
android:layout_alignLeft="@+id/age"
android:layout_alignTop="@+id/textView4" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView4"
android:layout_marginTop="32dp"
android:text="ADDRESS"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/submit"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/textView5"
android:ems="10"
android:inputType="textMultiLine"
/>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/age"
android:layout_marginTop="22dp"
android:text="GENDER"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
这是我的代码来初始化这些视图..
public class MainActivity extends ActionBarActivity implements OnClickListener {
EditText name,age,address;
Button submit;
Spinner gender;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.age, new PlaceholderFragment()).commit();
}
//Actual Code
initialise();
}
private void initialise() {
// TODO Auto-generated method stub
name=(EditText)findViewById(R.id.name);
age=(EditText)findViewById(R.id.age);
address=(EditText)findViewById(R.id.address);
submit=(Button)findViewById(R.id.submit);
gender=(Spinner)findViewById(R.id.gender);
submit.setOnClickListener(this);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.gender, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
gender.setAdapter(adapter);
}
}
答案 0 :(得分:0)
尝试关闭并重新启动IDE和模拟器。这个问题似乎很常见:
Android ClassCastException for RadioGroup into RadioButton
android.widget.RadioButton cannot be cast to android.widget.Spinner
ClassCastException: android.widget.EditText
这些问题始终由以下一项或多项解决: