最近我收到了来自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是否有任何错误导致它崩溃?
答案 0 :(得分:1)
LayoutParams params
您使用FrameLayout.LayoutParams并且您的视图需要RelativeLayout.LayoutParams,因为它的容器似乎是RelativeLayout。
更改布局参数的导入,从FrameLayout更改为RelativeLayout