To start with, some background about what I’m trying to do. I am programmatically (at runtime) removing a ScrollView’s child element and replacing it with a LinearLayout, and adding what used to be the ScrollView’s child element as a child of the LinearLayout. However, in order to get what I’m trying to achieve, I have to deal with setting up the LinearLayout I create. At minimum, I realize I have to set the LinearLayout’s orientation and change some size settings. Unfortunately, when I use LinearLayout’s setLayoutParams() method, it ends up crashing my app somewhere later (this code runs from onCreate, but the stack trace doesn’t show onCreate). Here is the relevant code:
parentContainer = new LinearLayout(activity);
// So now we swap out our LinearLayout as the ScrollView's child and parent the ViewGroup to the LinearLayout.
scrollView.removeAllViews();
scrollView.addView(parentContainer);
parentContainer.setOrientation(LinearLayout.VERTICAL);
parentContainer.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
The line that is apparently (best guess) causing the problem is:
parentContainer.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
The stack trace shows:
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
at android.widget.ScrollView.onMeasure(ScrollView.java:291)
at android.view.View.measure(View.java:7703)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
at android.view.View.measure(View.java:7703)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
at android.view.View.measure(View.java:7703)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
at android.view.View.measure(View.java:7703)
at android.view.ViewRoot.performTraversals(ViewRoot.java:747)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1613)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4203)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
at dalvik.system.NativeStart.main(Native Method)
This leads me to think that I’m using the LayoutParams incorrectly, but the documentation for LinearLayout.LayoutParams and setLayoutParams don’t offer any clues, and the exception has no associated message (which is very poor design on Android’s end). I wanted to check if anyone else knew something obvious I’m doing wrong here. My next step is to debug into the Android source, but it’s difficult to get a hold of the exact version I need and very costly time-wise to debug through it.
So the question is, is there anything obviously wrong with what I’m trying to do, so that I don’t spend hours debugging some stupid usage error?
P.S.: The Android version I’m testing on/targeting is 1.6 (emulator).
You are not placing the LinearLayout within another LinearLayout so you get a class cast exception. Since you are placing it within a ScrollView which extends FrameLayout change that line to this:
Alternativly you can set the layout parameters when you add it to the view like this: