sandalone
sandalone

Reputation: 41749

Why I cannot set LinearLayout's width programatically?

This piece of code is killing me. I am trying to set the width and height of a LinearLayout (containing children) programatically and the app is breaking. This is the code:

LinearLayout ll = (LinearLayout) findViewById(R.id.right_t);
ll.setLayoutParams(new android.widget.LinearLayout.LayoutParams(200, 200));

As you see, I made 100% sure that I can using the right layoutparams. Yet the app breaks with error

1-22 19:54:13.135: ERROR/AndroidRuntime(4560): FATAL EXCEPTION: main
    java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
    at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1317)
    at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1264)
    at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:292)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:314)
    at android.view.View.measure(View.java:12863)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
    at android.view.View.measure(View.java:12863)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
    at android.view.View.measure(View.java:12863)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2256)
    at android.view.View.measure(View.java:12863)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1197)
    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2585)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4507)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
    at dalvik.system.NativeStart.main(Native Method)

This LinearLayout with children is set in its own XML file and it is loaded in the main XML via <include> tag.

Any ideas? Why does it say that I am trying to LinearLayout to RelativeLayout when I am not doing it?

Upvotes: 0

Views: 2095

Answers (1)

Neil
Neil

Reputation: 468

The type of the LayoutParams is corresponded to the type of the parent layout in which you want to add the LinearLayout. So I guess in this case the parent layout is a RelativeLayout and you should create a RelativeLayout.LayoutParams instead of LinearLayout.LayoutParams.

Upvotes: 5

Related Questions