title: ClassCastException from View objects in android studio
body: <p>In Android Studio, when i try to run my project i get a <code>ClassCastException</code> as following:</p>
  
  <p><code>Caused by: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.RelativeLayout</code></p>
  
  <p>when the View object is 100% RelativeLayout and not EditText.
  The problem occurs on another View element if i comment out the above-related code..</p>
  
  <p>I have tried cleaning and rebuilding the project, i have tried editing the XML file and change the ID, i have tried deleting the XML file and restarting Android Studio and then make a new XML file, all that to no avail.</p>
  
  <p>Thank you for your help.</p>
  
  <p><strong>EDIT</strong>
  When i add new views to the xml, with id that i never used, somehow the activity before it gets nulls on its own (and totally seperate) xml file.. this is weird because theres no reason why a simple view on a different xml file should impact another xml that is loaded even before it..</p>
  
  <p><strong>EDIT2</strong>
  I inspected <code>View rl = activityView.findViewById(R.id.notification);</code> and found out that it gets etMessage which is another view element in the same XML and is infact an <code>EditText</code> type view.</p>
  
  <p>The code before the exception occurs:</p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setInitialView();
  }
  
  private void setInitialView() {
      RelativeLayout frame = (RelativeLayout) findViewById(R.id.activity_frame);
      LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      View activityView = layoutInflater.inflate(R.layout.chat, null,false);
      frame.removeAllViews();
      frame.addView(activityView);
  
      chatMap.put("Public", new ArrayList&lt;ChatMessageBox&gt;());
  
      // move the notification layout outside the screen
      RelativeLayout rl = (RelativeLayout) findViewById(R.id.notification);
      TranslateAnimation moveAnim = new TranslateAnimation(
              TranslateAnimation.ABSOLUTE, 0, TranslateAnimation.ABSOLUTE, 0,
              TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, -1);
      moveAnim.setDuration(0);
      moveAnim.setFillAfter(true);
      rl.startAnimation(moveAnim);
  }
  </code></pre>
  
  <p>This activity's parent XML, which is loaded with setContentView in its onCreate:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!--  Making the drawer layout the root view --&gt;
  &lt;android.support.v4.widget.DrawerLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/drawer_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"&gt;
  &lt;!-- MAIN CONTENT --&gt;
  
  &lt;RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/activity_frame"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" &gt;
  
  &lt;/RelativeLayout&gt;
  
  
  &lt;!-- The ListView to be displayed when the drawer layout is active --&gt;
  &lt;ListView android:id="@+id/left_drawer"
      android:layout_width="240dp"
      android:layout_height="match_parent"
      android:layout_gravity="start"
      android:choiceMode="singleChoice"
      android:divider="@android:color/transparent"
      android:dividerHeight="0dp"
      android:background="#111"/&gt;
  &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>
  
  <p>And the XML that has the appropriate view:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!-- Making the drawer layout the root view --&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:android1="http://schemas.android.com/apk/res/android"
  android:id="@+id/chat_relative_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  &gt;
  
  &lt;ScrollView
      android:id="@+id/scroller"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_above="@+id/bSendMessage"
      android:layout_alignParentLeft="true"
      android:layout_alignParentRight="true"
      android:background="@drawable/chat_bg" &gt;
  
      &lt;LinearLayout
          android:id="@+id/chatWindowContainer"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" &gt;
      &lt;/LinearLayout&gt;
  &lt;/ScrollView&gt;
  
  &lt;Button
      android:id="@+id/bSendMessage"
      android:layout_width="40dp"
      android:layout_height="40dp"
      android:layout_alignParentBottom="true"
      android:layout_alignParentRight="true"
      android:background="@drawable/ic_send" /&gt;
  
  &lt;EditText
      android:id="@+id/etMessage"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_alignParentLeft="true"
      android:layout_alignTop="@+id/bSendMessage"
      android:layout_toLeftOf="@+id/bSendMessage"
      android:paddingLeft="10dp"
      android:background="@drawable/text"
      android:textColor="#000000"
      android:imeOptions="actionNone"
      android:inputType="textMultiLine"
      android:maxLines="4"
      android:singleLine="true"
      android:layout_alignParentRight="false"
      android:layout_alignWithParentIfMissing="false" /&gt;
  
      &lt;!-- the desired element --&gt;
  &lt;RelativeLayout
      android:id="@+id/notification"
      android:layout_width="fill_parent"
      android:layout_height="30dp"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:background="@drawable/bg_upload"
      &gt;
  
      &lt;TextView
          android:id="@+id/tvNotification"
          android:layout_gravity="center"
          android:text="New Message Recieved"
          android:textAppearance="?android:attr/textAppearanceLarge"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true"
          android:textColor="#000000" /&gt;
  
  &lt;/RelativeLayout&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Error log:</p>
  
  <pre><code>java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.raad/com.example.raad.activities.ChatActivity}: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.RelativeLayout
                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
                                       at android.app.ActivityThread.access$600(ActivityThread.java:165)
                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
                                       at android.os.Handler.dispatchMessage(Handler.java:107)
                                       at android.os.Looper.loop(Looper.java:194)
                                       at android.app.ActivityThread.main(ActivityThread.java:5391)
                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                       at java.lang.reflect.Method.invoke(Method.java:525)
                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
                                       at dalvik.system.NativeStart.main(Native Method)
                                    Caused by: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.RelativeLayout
                                       at com.example.raad.activities.ChatActivity.setInitialView(ChatActivity.java:336)
                                       at com.example.raad.activities.ChatActivity.onCreate(ChatActivity.java:43)
                                       at android.app.Activity.performCreate(Activity.java:5122)
                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146)
                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403) 
                                       at android.app.ActivityThread.access$600(ActivityThread.java:165) 
                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373) 
                                       at android.os.Handler.dispatchMessage(Handler.java:107) 
                                       at android.os.Looper.loop(Looper.java:194) 
                                       at android.app.ActivityThread.main(ActivityThread.java:5391) 
                                       at java.lang.reflect.Method.invokeNative(Native Method) 
                                       at java.lang.reflect.Method.invoke(Method.java:525) 
                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
                                       at dalvik.system.NativeStart.main(Native Method)
  </code></pre>