Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8387973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:21:45+00:00 2026-06-09T18:21:45+00:00

To start with, some background about what I’m trying to do. I am programmatically

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:21:47+00:00Added an answer on June 9, 2026 at 6:21 pm

    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:

    parentContainer.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
    

    Alternativly you can set the layout parameters when you add it to the view like this:

    scrollView.addView(parentContainer, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to start writing some simple jQuery plugins and for my first
Let me start with some background: By tribool I understand a variable which can
First, some background: I've decided to start a new project designed from the ground
When a user visits an .aspx page, I need to start some background calculations
To start with some background, I am a member of a small team developing
I'll start with a little background. So what I'm trying to do is grabbing
I have some code that starts a background process for search in my WPF
I am planning to start some Java mobile phone development. I downloaded the wireless
I wrote an init script to execute last that will start some pythjon script.
I'll start with some classes... The Domain Entity: public class Account { public int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.