oreo - android pie
LinearLayout $ LayoutParams no se puede convertir a android.widget.FrameLayout $ LayoutParams (5)
Intenta limpiar tu proyecto. Debe haber copiado un bloque de xml de un FrameLayout y haber cambiado la etiqueta. Este es un error desagradable en Android.
Hubo un problema similar en otra discusión. Espero que esta sea la misma causa.
Explicación y solución en la parte inferior.
Estoy desarrollando una animación de diseño de control deslizante, la animación funciona bien, pero cuando todo el proceso termina, obtengo la siguiente Excepción.
Supongo que el padre RelativeLayout
tiene algo que ver con la excepción, pero no sé si lo resolvió.
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>
Actividad
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) {
}
}
Gracias de antemano.
EXPLICACIÓN Y SOLUCIÓN
Tenemos que conocer el contenedor padre ViewGroup para la vista, ya que las vistas se llenan con su LayoutParams primario para fines de medición.
Si no conocemos con un simple vistazo en el xml que es el Grupo de Vista principal para la Vista, podemos obtener siempre su referencia con el método .getParent () de la instancia de la Vista.
Me encontré con un problema similar y encontré la respuesta aquí . Básicamente, debe elegir los LayoutParams en función del elemento principal, en su caso el new LinearLayout.LayoutParams
debe ser el new RelativeLayout.LayoutParams
.
No debe hacer que el objeto LayoutParmas del diseño cuya dimensión desee establecer desde java. En su lugar, debe crear el objeto LayoutParams del diseño principal. Por ejemplo, en el diseño siguiente para establecer los parámetros de diseño de LinearLayout, debe hacer que LayoutParmas of RelativeLayout no LinearLayout sí mismo
<?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
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(30,30); //Parent Params
linearLayout.setLayoutParmas(params); //Child Layout
//If you want to set the params of the Relative Layout. It seems it doesn''t have any parent but it has a parent created by android framework which is a frame layout. So
FrameLayout.LayoutParmas params=new FrameLayout.LayoutParmas(30, 30);//FrameLayout is a parentcreated by anroid frame work
relativeLayout.setLayoutParmas(params); ////Here Relative layout is child
Por encima del error debido al paso de params de lanzamiento incorrectos: - porque alguna vez configura el parámetro programáticamente para el diseño lineal, pero cuando hace getLayoutParams () entonces algún tiempo le da una referencia de diseño principal y luego da el error anterior.
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);
Prueba esto
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(this.mainview.getWidth(), this.mainview.getHeight());
params.setMargins(left, top, right, bottom);
this.mainview.setLayoutParams(params);
Esto puede ser de ayuda