0

Here is my layout file. When I try to run this android app on my emulator It crashes and in the logcat I get the logcat error as :

classcastexception: android.Widget.EditText cannot be cast  to android.View.ViewGroup. 

I searched on google for this error and found out that this occurs when a view is not properly closed. But I checked and didnt find any mistakes.Any help and advice is appreciated. Thank you.

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

And here is my code to initialise these views..

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

}

Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
Nikhil Gopinath
  • 170
  • 1
  • 12

1 Answers1

0

Try shutting down and restarting your IDE and emulator. This issue seems to be pretty common:

Android ClassCastException for RadioGroup into RadioButton

android.widget.RadioButton cannot be cast to android.widget.Spinner

ClassCastException: android.widget.EditText

These issues are always resolved by one or more of the following:

  • clean and build project
  • restart IDE
  • restart emulator
Community
  • 1
  • 1
bstar55
  • 3,542
  • 3
  • 20
  • 24