Getting Java.lang.classcastexception: Android.widget.framelayout$layoutparams Cannot Be Cast To Android.widget.relativelayout$layoutparams
Solution 1:
The Error is right and you are also right,but its a misinterpretation on both you and android's part. You see when you Inflate a View with Parent_View being any sub class of a ViewGroup, android returns that View and treat it like a View if you want to treat it like a subclass of a ViewGroup you are going to have some cast Excetpions... You see i know this by experience but i cant give you a documented fact about this
Back to your question; you try to get the Layoutparams of your RelativeLayout which is the top most View of that xml, so i am thinking getLayoutParams() should return null since its not attached to a ViewGroup but its not which means its attached to a FrameLayout which is the Top most View for the Window or itself.
To solve it
try this (1)
instead of using this line
RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams)
contentLayout.getLayoutParams();
use this
//get the layoutParams of the TextView child in the Relativelayout in //your xml,this will automatically map to the RelativeLayout params
RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams)
displayTextView.getLayoutParams();
or use this(2)
change only your xml, and your previous code will work fine
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"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">
<RelativeLayout
android:id="@+id/content_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/display_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>
Post a Comment for "Getting Java.lang.classcastexception: Android.widget.framelayout$layoutparams Cannot Be Cast To Android.widget.relativelayout$layoutparams"