java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams

爱⌒轻易说出口 提交于 2019-12-05 03:23:24

In your code you are importing import android.widget.RelativeLayout.LayoutParams; instead of android.view.ViewGroup.LayoutParams

so Delete import android.widget.RelativeLayout.LayoutParams;

and Add import android.view.ViewGroup.LayoutParams

Balman Rawat

You should make the LayoutParams object of the parent layout. Example in the layout below to set height and width of the LinearLayout you have to make the LayoutParmas of RelativeLayout not LinearLayout itself.

  <?xml version="1.0" encoding="utf-8"?>
    <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"
    tools:context=".newsblog.NewsDetailsActivity"
    >

      <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"/>


 </RelativeLayout>

Java:

//IN ACTIVITY
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(30,30); //Parent Params
linearLayout.setLayoutParmas(params); //Child Layout


//If you want to set the params of the Relative Layout above. It seems it doesn't have any parent but it has a parent created by android framework which is a frame layout. So 
FrameLayout.LayoutParmas params=new FrameLayout.LayoutParmas(30, 30);//FrameLayout is a parentcreated by anroid frame work
relativeLayout.setLayoutParmas(params);  ////Here Relative layout is child
praveen s

just click Cnt+o for delete unwanted imports

Use code below:

imageView.setLayoutParams(
  new android.widget.RelativeLayout.LayoutParams(
    ViewPager.LayoutParams.MATCH_PARENT,
    ViewPager.LayoutParams.MATCH_PARENT));

Do let me know if you face any error.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!