简体   繁体   English

ClassCastException:android.widget.RelativeLayout $ LayoutParams无法强制转换为android.widget.FrameLayout $ LayoutParams

[英]ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams

all,my situalation is that: activity contains viewpager, viewpager contains a few of fragments, and the main function of the fragment is playing video,and there are many functions about video,containing zooming the video,narrowing the video and so on.But when I trigger the function of zooming the video,the exception accurs: 总之,我的情况是:activity包含viewpager,viewpager包含一些片段,片段的主要功能是播放视频,有很多关于视频的功能,包括缩放视频,缩小视频等等。但是当我触发缩放视频的功能时,异常诅咒:

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

But when I use FrameLayout.LayoutParams,the exception also accurs,like this: 但是当我使用FrameLayout.LayoutParams时,异常也会令人沮丧,如下所示:

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

And the xml of root view of the fragment is: 并且片段的根视图的xml是:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
    android:id="@+id/header_wrapper"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/player_height">


    <RelativeLayout
        android:id="@+id/view_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true">

        <com.baidu.cyberplayer.core.BVideoView
            android:id="@+id/video_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/controlbar"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <Button
            android:id="@+id/play_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_gravity="center_vertical"
            android:background="@drawable/play_btn_style" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="15dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/time_current"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:paddingLeft="4dip"
                android:paddingRight="4dip"
                android:paddingTop="4dip"
                android:text="00:00"
                android:textColor="@color/play_time"
                android:textSize="14sp"
                android:textStyle="bold" />

            <SeekBar
                android:id="@+id/media_progress"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="0dip"
                android:layout_height="30dip"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/time_total"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:paddingLeft="4dip"
                android:paddingRight="4dip"
                android:paddingTop="4dip"
                android:text="00:00"
                android:textColor="@color/play_time"
                android:textSize="14sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/zoom_btn"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="@drawable/screensize_zoomout_button" />
        </LinearLayout>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/VideoAction"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true">

        <ImageView
            android:id="@+id/detailPic"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:adjustViewBounds="false"
            android:contentDescription="@null"
            android:scaleType="fitXY"
            android:src="@drawable/big_bg" />

        <ImageButton
            android:id="@+id/pre_play_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="#00000000"
            android:contentDescription="@null"
            android:src="@drawable/play_btn_style"
            android:visibility="invisible" />

        <com.basv.gifmoviewview.widget.GifMovieView
            android:id="@+id/loading_gif"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            app:gif="@drawable/big_loading" />
    </RelativeLayout>
</RelativeLayout>

The code of function that zoom the video is: 缩放视频的功能代码是:

    RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
            Screen.getScreenWidth(getActivity().getWindowManager()),
            Screen.getScreenHeight(getActivity().getWindowManager()));
    mHeaderWrapper.setLayoutParams(param);
    mVV.setLayoutParams(param);
    mZoomButton.setBackgroundResource(R.drawable.screensize_zoomin_button);
    mCurrentScape = OrientationHelper.LANDSCAPE;

I think the exception accurs at the position of the code above: 我认为上述代码位置的异常诅咒:

    RelativeLayout.LayoutParams param = new  RelativeLayout.LayoutParams(
    Screen.getScreenWidth(getActivity().getWindowManager()),
    Screen.getScreenHeight(getActivity().getWindowManager()));

I want to realize the function: zoom and narrow the video,that,make the video in fragment full screen in activity and can make the video shrink. 我想实现这个功能:缩放和缩小视频,使视频在片段全屏显示活动并使视频缩小。

Or,Which kind of LayoutParams should I use? 或者,我应该使用哪种LayoutParams?

As you know,the question occurs that "mHeaderWrapper.setLayoutParams(param);",and I use the method "mHeaderWrapper.getParent" to know the parent of the layout mHeaderWrapper ,it is 如您所知,问题出现在“mHeaderWrapper.setLayoutParams(param);”,并且我使用方法“mHeaderWrapper.getParent”来了解布局mHeaderWrapper的父级 ,它是

" android.support.v4.app.NoSaveStateFrameLayout ", android.support.v4.app.NoSaveStateFrameLayout ”,

but there is no android.support.v4.app.NoSaveStateFrameLayout.LayoutParams,how to handle with it? 但是没有android.support.v4.app.NoSaveStateFrameLayout.LayoutParams,如何处理呢?

Thank you! 谢谢!

How to deal with the ClassCastException 如何处理ClassCastException

1) Be careful when trying to cast an object of a class into another class. 1)尝试将类的对象转换为另一个类时要小心。 Ensure that the new type belongs to one of its parent classes. 确保新类型属于其父类之一。

2) You can prevent the ClassCastException by using Generics, because Generics provide compile time checks and can be used to develop type-safe applications. 2)您可以使用Generics来防止ClassCastException,因为Generics提供了编译时检查,可用于开发类型安全的应用程序。

Refrence link 参考链接

http://examples.javacodegeeks.com/java-basics/exceptions/java-lang-classcastexception-how-to-solve-class-cast-exception/ http://examples.javacodegeeks.com/java-basics/exceptions/java-lang-classcastexception-how-to-solve-class-cast-exception/

in Your case 在你的情况下

may b im wrong but i think you should use framelayout .. 可能我错了,但我认为你应该使用framelayout ..

I think exception happens when you set layoutparams to mheaderwrapper. 我认为当你将layoutparams设置为mheaderwrapper时会发生异常。 What's its parent? 它的父母是什么?

Try changing 尝试改变

RelativeLayout.LayoutParams param = new  RelativeLayout.LayoutParams(
    Screen.getScreenWidth(getActivity().getWindowManager()),
    Screen.getScreenHeight(getActivity().getWindowManager()));

to

FrameLayout.LayoutParams param = new  FrameLayout.LayoutParams(
    Screen.getScreenWidth(getActivity().getWindowManager()),
    Screen.getScreenHeight(getActivity().getWindowManager()));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.FrameLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams 获取java.lang.ClassCastException:android.widget.FrameLayout $ LayoutParams无法强制转换为android.widget.RelativeLayout $ LayoutParams - Getting java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams ClassCastException:android.widget.LinearLayout$LayoutParams 不能转换为 android.widget.FrameLayout$LayoutParams - ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams LinearLayout$LayoutParams 不能转换为 android.widget.FrameLayout$LayoutParams - LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.LinearLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams java.lang.ClassCastException:android.view.ViewGroup$LayoutParams 不能转换为 android.widget.RelativeLayout$LayoutParams - java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams Java.lang.ClassCastException:无法将android.widget.RelativeLayout强制转换为android.widget.FrameLayout - Java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.FrameLayout java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法转换为android.widget.FrameLayout - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout ClassCastException:无法将android.widget.RelativeLayout $ LayoutParams强制转换为Activity内部的android.support.v7.widget.RecyclerView $ LayoutParams - ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams inside activity java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法强制转换为android.support.v7.widget.RecyclerView $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM