java.lang.ClassCastException:android.view.ViewGroup$LayoutParams不能强制转换为android.widget.RelativeLayout$LayoutParams
我刚刚编辑了我的XML,我在XML中添加了幻灯片菜单代码,但是有一个错误java.lang.ClassCastException:android.view.ViewGroup$LayoutParams不能强制转换为android.widget.RelativeLayout$LayoutParams,java,android,android-layout,android-relativelayout,layoutparams,Java,Android,Android Layout,Android Relativelayout,Layoutparams,我刚刚编辑了我的XML,我在XML中添加了幻灯片菜单代码,但是有一个错误 <?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_pa
<?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"
android:background="#ffffff" >
<ListView
android:id="@+id/menu_content_menulist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:cacheColorHint="@android:color/transparent"
android:divider="@null"
android:listSelector="@null"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mozaik.tangsel.side_menu_scroll.ScrollerLinearLayout
android:id="@+id/menu_content_side_slide_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="verti_
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/content_statis"
android:orientation="vertical"
android:background="@drawable/bg_app"
>
<include layout="@layout/layout_action_bar" />
<ScrollView
android:id="@+id/SV"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:background="@drawable/inbox_interface"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/ImageMozaikTangsel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip" />
</LinearLayout>
<LinearLayout
android:id="@+id/tabMozaikTangselLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</com.mozaik.tangsel.side_menu_scroll.ScrollerLinearLayout>
</RelativeLayout>
</RelativeLayout>
在您的代码中,您正在导入
import android.widget.RelativeLayout.LayoutParams代码>而不是android.view.ViewGroup.LayoutParams
因此,删除导入android.widget.RelativeLayout.LayoutParams代码>
并添加导入android.view.ViewGroup.LayoutParams
只需单击Cnt+o删除不需要的导入您应该将LayoutParams对象设置为父布局。下面布局中的示例要设置LinearLayout的高度和宽度,必须使RelativeLayout的布局参数不是LinearLayout本身
<?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>
使用以下代码:
imageView.setLayoutParams(
new android.widget.RelativeLayout.LayoutParams(
ViewPager.LayoutParams.MATCH_PARENT,
ViewPager.LayoutParams.MATCH_PARENT));
如果遇到任何错误,请务必通知我。看起来您正在修改代码中的
布局参数。XML布局本身不会导致这种情况。因此,请在问题中包含相关代码。请确保导入正确的LayoutParams。我的错误已通过@kalyan pvs的回答解决,谢谢:)@bukanamay Good,那么你必须接受他的答案,这里需要帮助为什么ViewGroup.LayoutParams会起作用?@IgorGanapolsky因为LayoutParams是特定类的内部类..RelativeLayout扩展ViewGroup所以你可以进行类型转换,但是RelativeLayout.LayoutParams不能扩展ViewGroup.LayoutParams所以它会抛出classcastexception等等这是一个很好的答案。
//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
imageView.setLayoutParams(
new android.widget.RelativeLayout.LayoutParams(
ViewPager.LayoutParams.MATCH_PARENT,
ViewPager.LayoutParams.MATCH_PARENT));