Join FlipAndroid.COM Telegram Group: https://t.me/joinchat/F_aqThGkhwcLzmI49vKAiw


ClassCastException cuando se utiliza LinearLayout.LayoutParams

Esta es la excepción:

07-20 19:52:14.193: E/AndroidRuntime(3908): FATAL EXCEPTION: main 07-20 19:52:14.193: E/AndroidRuntime(3908): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.widget.LinearLayout.measureVertical(LinearLayout.java:634) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.View.measure(View.java:12892) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.View.measure(View.java:12892) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1249) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.View.measure(View.java:12892) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.View.measure(View.java:12892) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.View.measure(View.java:12892) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 07-20 19:52:14.193: E/AndroidRuntime(3908): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2265) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.View.measure(View.java:12892) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1240) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.os.Handler.dispatchMessage(Handler.java:99) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.os.Looper.loop(Looper.java:137) 07-20 19:52:14.193: E/AndroidRuntime(3908): at android.app.ActivityThread.main(ActivityThread.java:4507) 07-20 19:52:14.193: E/AndroidRuntime(3908): at java.lang.reflect.Method.invokeNative(Native Method) 07-20 19:52:14.193: E/AndroidRuntime(3908): at java.lang.reflect.Method.invoke(Method.java:511) 07-20 19:52:14.193: E/AndroidRuntime(3908): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980) 07-20 19:52:14.193: E/AndroidRuntime(3908): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747) 07-20 19:52:14.193: E/AndroidRuntime(3908): at dalvik.system.NativeStart.main(Native Method) 

No puedo ver una línea donde la excepción es lanzada así que no puedo entenderlo muy bien. Sé que es en estas líneas:

 public class TodayFragment extends Fragment { ListView listView; String[] urls; ActionBar mActionBar; Spinner spinner1; public TodayFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { LinearLayout linear = new LinearLayout(getActivity()); linear.setOrientation(LinearLayout.VERTICAL); spinner1 = new Spinner(getActivity()); spinner1.setLayoutParams(new LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); linear.addView(spinner1); listView = new ListView(getActivity()); spinner1.setLayoutParams(new LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); linear.addView(listView); new ParseHTML().execute(); return linear; } } 

El fragmento que estoy usando es android.support.v4.app.Fragment. ¿Qué causa esto?

3 Solutions collect form web for “ClassCastException cuando se utiliza LinearLayout.LayoutParams”

Su declaración de importación para LayoutParams probablemente está importando ViewGroup.LayoutParams. Intente utilizar explícitamente LinearLayout.LayoutParams (…):

  spinner1.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

Trate de configurar LayoutParams de LinearLayout antes de asignar a los niños.

 LinearLayout linear = new LinearLayout(getActivity()); linear.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); linear.setOrientation(LinearLayout.VERTICAL); 

En caso de que te preguntes qué LayoutParams debes utilizar para cada vista, mira este documento :

Cada vista debe tener un LayoutParam de acuerdo con su padre.

Por ejemplo, para crear esta jerarquía de Vistas,

  • RelativeLayout relativeLayout
    • LinearLayout linearLayout, orientación: vertical
    • TextView textView
    • Botón button1
    • Botón button2

Reemplace el contenido de su método onCreate en su actividad principal con:

  super.onCreate(savedInstanceState); RelativeLayout relativeLayout = new RelativeLayout(this); LinearLayout linearLayout = new LinearLayout(this); linearLayout.setOrientation(LinearLayout.VERTICAL); linearLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)); relativeLayout.addView(linearLayout); TextView textView = new TextView(this); textView.setText("a text"); textView.setLayoutParams(new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); linearLayout.addView(textView); Button button1 = new Button(this); button1.setText("button 1"); button1.setLayoutParams(new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); linearLayout.addView(button1); setContentView(relativeLayout); 
FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.