title: Getting java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
body: <p>Here I need to add textview at runtime. first I took reference of RelativeLayout to get layout params but unfortunately its throwing ClassCastException at <strong>RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams)contentLayout.getLayoutParams()</strong></p>
  
  <p>I also checked - <a href="https://stackoverflow.com/questions/11544964/framelayout-to-relativelayout-classcastexception-even-if-there-is-no-framelayout">FrameLayout to RelativeLayout ClassCastException even if there is no FrameLayout used</a>, but it didn't work. Since after replacing RelativeLayout with FrameLayout, I'm unable to add rule on layoutParams. </p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
  
              TextView displayTextView = (TextView)findViewById(R.id.display_text);
  
              ContentResolver resolver = getContentResolver();
              Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI, null, null, null, null);
  
              cursor.moveToFirst();
              displayTextView.setText("The User Dictionary contains " + cursor.getColumnCount() + " words");
  
              RelativeLayout contentLayout = (RelativeLayout)findViewById(R.id.content_layout);
  
              TextView columnNames = new TextView(this);
              columnNames.setText("Columns: _id - frequency - word");
              contentLayout.addView(columnNames);
  
              RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams)contentLayout.getLayoutParams();
              params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
              params.addRule(RelativeLayout.BELOW);
  
              do{
                  TextView entry = new TextView(this);
                  entry.setText(cursor.getInt(cursor.getColumnIndex(UserDictionary.Words._ID)) + " - " +
                                cursor.getInt(cursor.getColumnIndex(UserDictionary.Words.FREQUENCY)) + " - " +
                                cursor.getString(cursor.getColumnIndex(UserDictionary.Words.WORD)));
  
                  entry.setLayoutParams(params);
                  contentLayout.addView(entry);
              }while(cursor.moveToNext());
  
              cursor.close();
      }
  
      **Getting following error** - 
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dexter.contentpro/com.example.dexter.contentpro.MainActivity}: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                  at android.app.ActivityThread.access$800(ActivityThread.java:144)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5221)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
           Caused by: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
                  at com.example.dexter.contentpro.MainActivity.onCreate(MainActivity.java:37)
                  at android.app.Activity.performCreate(Activity.java:5933)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                  at android.app.ActivityThread.access$800(ActivityThread.java:144)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5221)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
  </code></pre>
  
  <p>activity_main.xml-</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/content_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"&gt;
  
      &lt;TextView
          android:id="@+id/display_text"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>