[英]LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
Explanation and solution at the bottom.底部的解释和解决方案。
I am developing one slider layout animation, the animation work fine but when all processes end, they get next Exception.我正在开发一个滑块布局动画,动画效果很好,但是当所有进程结束时,它们会得到下一个异常。
I guess RelativeLayout
parent have something to do in exception, but I don't know how to resolve it.我猜RelativeLayout
父在异常中有一些事情要做,但我不知道如何解决它。
09-06 11:24:58.952: E/Trace(30884): error opening trace file: No such file or directory (2)
09-06 11:25:09.113: E/AndroidRuntime(30884): FATAL EXCEPTION: main
09-06 11:25:09.113: E/AndroidRuntime(30884): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.widget.FrameLayout.onMeasure(FrameLayout.java:311)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.view.View.measure(View.java:15264)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.view.View.measure(View.java:15264)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.view.View.measure(View.java:15264)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-06 11:25:09.113: E/AndroidRuntime(30884): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2161)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.view.View.measure(View.java:15264)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2129)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1240)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1433)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1125)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4607)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:747)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.view.Choreographer.doCallbacks(Choreographer.java:567)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.view.Choreographer.doFrame(Choreographer.java:536)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:733)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.os.Handler.handleCallback(Handler.java:615)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.os.Handler.dispatchMessage(Handler.java:92)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.os.Looper.loop(Looper.java:153)
09-06 11:25:09.113: E/AndroidRuntime(30884): at android.app.ActivityThread.main(ActivityThread.java:5086)
09-06 11:25:09.113: E/AndroidRuntime(30884): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 11:25:09.113: E/AndroidRuntime(30884): at java.lang.reflect.Method.invoke(Method.java:511)
09-06 11:25:09.113: E/AndroidRuntime(30884): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
09-06 11:25:09.113: E/AndroidRuntime(30884): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
09-06 11:25:09.113: E/AndroidRuntime(30884): at dalvik.system.NativeStart.main(Native Method)
activity_home.xml活动主页.xml
<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" >
<LinearLayout
android:id="@+id/leftView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cad000"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/mainView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#876000"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#cecece"
android:orientation="horizontal" >
<Button
android:id="@+id/btnSlide"
style="@style/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Activity活动
public class HomeActivity extends Activity implements OnClickListener {
UserStorage userStorage = new UserStorage();
private Button btnSlide;
private LinearLayout mainView, leftView;
private SliderAnimation slideAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
this.leftView = (LinearLayout) findViewById(R.id.leftView);
this.mainView = (LinearLayout) findViewById(R.id.mainView);
this.btnSlide = (Button) findViewById(R.id.btnSlide);
this.btnSlide.setOnClickListener(this);
this.slideAnimation = new SliderAnimation(this);
this.slideAnimation.initializeFilterAnimations(this.mainView, this.leftView);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSlide:
this.slideAnimation.toggleLeftSliding();
break;
}
}
}
AnimationListener动画监听器
public class SliderAnimation implements AnimationListener {
private Context context;
private LinearLayout mainView, leftView;
private Animation mainSlideIn, mainSlideOut;
private boolean leftAnimated, rightAnimated = false;
private int deviceWidth;
public SliderAnimation(Context context) {
this.context = context;
DisplayMetrics displayMetrics = context.getResources()
.getDisplayMetrics();
this.deviceWidth = displayMetrics.widthPixels;
}
public void initializeFilterAnimations(LinearLayout mainView,
LinearLayout leftView) {
this.mainView = mainView;
this.leftView = leftView;
this.mainSlideIn = AnimationUtils.loadAnimation(context,
R.anim.main_slide_in);
this.mainSlideIn.setAnimationListener(this);
this.mainSlideOut = AnimationUtils.loadAnimation(context,
R.anim.main_slide_out);
this.mainSlideOut.setAnimationListener(this);
}
public void toggleLeftSliding() {
if (!this.leftAnimated) {
this.mainView.startAnimation(this.mainSlideIn);
} else {
this.mainView.startAnimation(this.mainSlideOut);
}
}
@Override
public void onAnimationEnd(Animation animation) {
if (!this.leftAnimated) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
(this.deviceWidth * 20) / 100, this.mainView.getHeight());
params.leftMargin = (this.deviceWidth * 80) / 100;
this.mainView.setLayoutParams(params);
this.leftAnimated = true;
} else {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
this.deviceWidth, this.mainView.getHeight());
params.leftMargin = 0;
this.mainView.setLayoutParams(params);
this.leftAnimated = false;
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}
}
Thank you in advance.先感谢您。
EXPLANATION AND SOLUTION解释和解决方案
We've to know the parent ViewGroup container for the view, because views are filled with their parent LayoutParams for measure purposes.我们必须知道视图的父 ViewGroup 容器,因为出于测量目的,视图填充了它们的父 LayoutParams。
If we don't know through a simple glance on the xml which is the parent ViewGroup for the View, we can get always its reference with .getParent() method from the View instance.如果我们不知道通过简单的 xml 是 View 的父 ViewGroup,我们总是可以从 View 实例中使用 .getParent() 方法获取它的引用。
I ran into a similar problem and I found the answer here .我遇到了类似的问题,我在这里找到了答案。 Basically, you should choose the LayoutParams depending on the parent, in your case new LinearLayout.LayoutParams
should be new RelativeLayout.LayoutParams
.基本上,您应该根据父级选择 LayoutParams,在您的情况下new LinearLayout.LayoutParams
应该是new RelativeLayout.LayoutParams
。
You should not make the LayoutParmas
object of the layout whose dimension you want to set from java.你不应该让布局的LayoutParmas
对象你想从 java 设置其尺寸。 Rather you should make the LayoutParams
object of the parent layout.相反,您应该创建父布局的LayoutParams
对象。 For example in the layout below to set the layout params of the LinearLayout
you have to make the LayoutParmas
of RelativeLayout
not LinearLayout
itself:例如,在下面的布局中,要设置LinearLayout
的布局参数,您必须使RelativeLayout
的LayoutParmas
不是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>
In activity class:在活动课上:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30,30); // parent params
linearLayout.setLayoutParmas(params); // child layout
If you want to set the params of the RelativeLayout
, it seems it doesn't have any parent but it has a parent created by android framework which is a frame layout:如果你想设置RelativeLayout
的参数,它似乎没有任何父级,但它有一个由 android 框架创建的父级,它是一个框架布局:
FrameLayout.LayoutParmas params = new FrameLayout.LayoutParmas(30, 30); // FrameLayout is a parent-created by android framework
relativeLayout.setLayoutParmas(params); // relative layout is child here
Try doing a clean of your project.尝试清理您的项目。 You must have copy pasted a block of xml from a FrameLayout and changed the tag.您必须从 FrameLayout 复制粘贴一块 xml 并更改了标记。 This is a nasty bug in android.这是android中的一个令人讨厌的错误。
There was a similar issue on another discussion.在另一个讨论中也有类似的问题。 I hope this is the same cause.我希望这是同样的原因。
Try this:试试这个:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(this.mainview.getWidth(), this.mainview.getHeight());
params.setMargins(left, top, right, bottom);
this.mainview.setLayoutParams(params);
This may be help.这可能有帮助。
Above error due to passing wrong casting params :- because some time you set programattically parameter for linear layout but when you doing getLayoutParams() then it some time gives parent layout reference then it is gives above error .由于传递错误的强制转换参数而导致上述错误:- 因为有时您以编程方式为线性布局设置参数,但是当您执行 getLayoutParams() 时,它有时会提供父布局参考,然后会出现上述错误。
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) parentLayout.getLayoutParams();
int leftMargin = (int) getResources().getDimension(R.dimen.exclusion_card_margin_left_right);
int rightMargin = (int) getResources().getDimension(R.dimen.exclusion_card_margin_left_right);
params.setMargins(leftMargin, 0, rightMargin, 0);
parentLayout.setLayoutParams(params);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.