Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassCastException when using LinearLayout.LayoutParams

This is the exception:

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)

I can't see a line where the exception is thrown so I can't figure it out quite good. I do know it is in these lines:

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

The fragment I'm using is android.support.v4.app.Fragment. What causes this?

like image 355
tolgap Avatar asked Jul 20 '12 18:07

tolgap


1 Answers

Your import statement for LayoutParams is probably importing ViewGroup.LayoutParams. Try explicitly using LinearLayout.LayoutParams(...):

   spinner1.setLayoutParams(new LinearLayout.LayoutParams(
           LinearLayout.LayoutParams.WRAP_CONTENT,
           LinearLayout.LayoutParams.WRAP_CONTENT));
like image 107
ab11 Avatar answered Oct 13 '22 13:10

ab11