Android ClassCastException:无法将FrameLayout强制转换为RelativeLayout

Android ClassCastException:无法将FrameLayout强制转换为RelativeLayout,android,exception,classcastexception,android-relativelayout,android-framelayout,Android,Exception,Classcastexception,Android Relativelayout,Android Framelayout,最近我从Google Play收到了一份奇怪的崩溃报告: Fatal Exception: java.lang.ClassCastExceptionandroid.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams 这是我的日志文件: java.lang.ClassCastException: android.widget.FrameLayout$LayoutP

最近我从Google Play收到了一份奇怪的崩溃报告:

Fatal Exception: java.lang.ClassCastExceptionandroid.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
这是我的日志文件:

java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
   at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1722)
   at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1667)
   at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:373)
   at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
   at android.view.View.measure(View.java:16525)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
   at android.view.View.measure(View.java:16525)
   at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
   at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
   at android.view.View.measure(View.java:16525)
   at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:940)
   at android.view.View.measure(View.java:16525)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
   at android.view.View.measure(View.java:16525)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
   at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
   at android.view.View.measure(View.java:16525)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
   at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
   at android.view.View.measure(View.java:16525)
   at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1955)
   at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1152)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1334)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1039)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5648)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
   at android.view.Choreographer.doCallbacks(Choreographer.java:574)
   at android.view.Choreographer.doFrame(Choreographer.java:544)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
   at android.os.Handler.handleCallback(Handler.java:733)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:136)
   at android.app.ActivityThread.main(ActivityThread.java:5111)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
   at dalvik.system.NativeStart.main(NativeStart.java)
现在我发现问题是在我的应用程序中调用此代码时引起的:

 public void setTopAlignment(boolean alignTop){

    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    if(alignTop){
        params.gravity = Gravity.TOP;
        int actionBarHeight = 0;
        TypedValue tv = new TypedValue();
        if (getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
        {
            actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getContext().getResources().getDisplayMetrics());
        }

        int statusBarHeight = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }

        params.setMargins(0, actionBarHeight + statusBarHeight, 0,0);
    }else{
        params.gravity = Gravity.BOTTOM;
    }
    MessageView.this.setLayoutParams(params);
}
MessageView类扩展了FrameLayout。因此,由于某种原因,当调用我的代码的这一部分时,ClassCastException出现了。是否有任何错误导致它崩溃

布局参数

使用FrameLayout.LayoutParams和视图所需的RelativeLayout.LayoutParams,因为其容器似乎是RelativeLayout


将布局参数的导入从FrameLayout更改为RelativeLayout

布局参数始终应用于父布局。你的MessageView的容器是什么?我的MessageView类的容器是FrameLayout。奇怪的是,我无法在我的设备上重现错误,我的设备是Motorola G。无论我多么努力地尝试,应用程序都不会因此异常而崩溃。如果我这样做,它会崩溃,并出现类似的错误,表示相反:Cant cast相对框架布局