gpt4 book ai didi

android - LinearLayout$LayoutParams 无法转换为 android.widget.FrameLayout$LayoutParams

转载 作者:IT王子 更新时间:2023-10-28 23:45:33 27 4
gpt4 key购买 nike

说明及解决方法在底部。

我正在开发一个 slider 布局动画,动画效果很好,但是当所有进程结束时,它们会得到下一个异常。

我猜 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

<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;
}
}
}

动画监听器

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) {
}
}

提前谢谢你。

解释和解决方案

我们必须知道 View 的父 ViewGroup 容器,因为 View 填充了它们的父 LayoutParams 用于测量目的。

如果我们无法通过简单的浏览来了解作为 View 的父 ViewGroup 的 xml,我们总是可以使用 .getParent() 方法从 View 实例中获取它的引用。

最佳答案

我遇到了类似的问题,我发现 the answer here .基本上,您应该根据父级选择 LayoutParams,在您的情况下 new LinearLayout.LayoutParams 应该是 new RelativeLayout.LayoutParams

关于android - LinearLayout$LayoutParams 无法转换为 android.widget.FrameLayout$LayoutParams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18655940/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com