6

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?

3 Answers 3

16

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));
Sign up to request clarification or add additional context in comments.

Comments

2

Try setting LinearLayout's LayoutParams' before attatching any children.

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

Comments

1

In case you're wondering which LayoutParams you must use for each View, look at this doc :

Each view must have a LayoutParam acording to his parent.

For example, in order to create this Views hierarhy,

  • RelativeLayout relativeLayout
    • LinearLayout linearLayout , orientation: vertical
    • TextView textView
    • Button button1
    • Button button2

replace the content of your onCreate method in your main activity with :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.