Java Question ClassCastException with LayoutParams

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Getting a java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams Error when using my library suddenly.

It had been working fine, but in one screen I have an Imageview which I add with a set size of the minimum of 50% X or 50% Y (This works fine) and then calculate its size later in Resume if there is a picture to show. The stack trace looks like it is failing when I get the Left for the Imageview.

My Class extends ViewWrapper<ScrollView>.

Then the function to add the view is:

B4X:
public void AddView(View NewView, String ViewName){
       if (myRelLayout == null) {
          myRelLayout= new RelativeLayout(ba.context);
          LayoutParams myLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            myRelLayout.setLayoutParams(myLayoutParams);
           getObject().addView(myRelLayout);
       }
       RelativeLayout.LayoutParams myLayoutParams = new RelativeLayout.LayoutParams(Width, Height);
       myLayoutParams.setMargins(ViewLeftPadding, ViewTopPadding, ViewRightPadding, ViewBottomPadding);
       if (xView.length() == 0) { // Assume Parent Align
          if (xAlign == getxAlign_ParentLeft()) myLayoutParams.addRule(getxAlign_ParentLeft());
          if (xAlign == getxAlign_ParentRight()) myLayoutParams.addRule(getxAlign_ParentRight());
          if (xAlign == getxAlign_ParentCentHorz()) myLayoutParams.addRule(getxAlign_ParentCentHorz());
       }
       else {
          if (viewList.containsKey(xView)){
             if (xAlign == getxAlign_ViewLeft()) myLayoutParams.addRule(getxAlign_ViewLeft(), viewList.get(xView).getId());
              if (xAlign == getxAlign_ViewRight()) myLayoutParams.addRule(getxAlign_ViewRight(), viewList.get(xView).getId());
              if (xAlign == getxAlign_LeftOfView()) myLayoutParams.addRule(getxAlign_LeftOfView(), viewList.get(xView).getId());
              if (xAlign == getxAlign_RightOfView()) myLayoutParams.addRule(getxAlign_RightOfView(), viewList.get(xView).getId());
          }
      }
       if (yView.length() == 0) { // Assume Parent Align
          if (yAlign == getyAlign_ParentTop()) myLayoutParams.addRule(getyAlign_ParentTop());
          if (yAlign == getyAlign_ParentCentVert()) myLayoutParams.addRule(getyAlign_ParentCentVert());
       }
       else {
          if (viewList.containsKey(yView)){
             if (yAlign == getyAlign_AboveView()) myLayoutParams.addRule(getyAlign_AboveView(), viewList.get(yView).getId());
              if (yAlign == getyAlign_BelowView()) myLayoutParams.addRule(getyAlign_BelowView(), viewList.get(yView).getId());
              if (yAlign == getyAlign_ViewTop()) myLayoutParams.addRule(getyAlign_ViewTop(), viewList.get(yView).getId());
              if (yAlign == getyAlign_ViewBottom()) myLayoutParams.addRule(getyAlign_ViewBottom(), viewList.get(yView).getId());
              if (yAlign == getyAlign_ViewBaseline()) myLayoutParams.addRule(getyAlign_ViewBaseline(), viewList.get(yView).getId());
          }
      }
       NewView.setLayoutParams(myLayoutParams);
       myRelLayout.addView(NewView);
       if (ViewName.length() > 0) viewList.put(ViewName, NewView);
    }

It should have a layout, but it is like B4A doesn't know how to read it or something since it is from RelativeLayout. Is there a way to fix this? When running in debug it is showing the Activity Layout not available, my Relative Layout (Scrollview) has a layout, then my Imageview Layout is not available. Putting breaks in other parts of the code shows the Activity and Imageview never get a layout.
 
Last edited:

Roger Garstang

Well-Known Member
Licensed User
Longtime User
That was the full error, the rest is the stack trace stuff showing it in Resume and trying to Get Left without a layout. I think one of the lines mentioned not being able to Cast RelativeLayout to B4ALayout.

The line it errors on in Resume is:

B4X:
partyPic.Width = Min(partyPic.Height * sizeRatio, 100%x - 8dip - partyPic.Left)

I do it and a few other lines to size the picture to a thumbnail fitting the screen if it exists. When the pic doesn't exist everything draws fine and I guess never has a layout...or at least not one B4A can read.

I tried a bunch of different layoutparam types, but nothing really did it. I even said the heck with it the last couple hours and copied all the code into a new Class I call LineLayout. It works even closer to my View Manager class because even when this draws if I make a normal flow of Label and Edit Text on each row, one is usually taller, and Relative only looks at one view for placement reference. The new class goes by lines and each added control size would bump up the NextX and NextY then when they call NextLine it would go down the needed space. Unfortunately I can't ever get the Width and Height of the Views after addView. I've read a bunch of articles on getting it with calling Measure() and using the measuredWidth/Height which people say was different than drawn, so won't help. I even tried monitoring the tree global layout event that many say works, but it just Force Closed and I need the results right away to keep the calculations for the next view while it sounds like an Event fired later (Possibly even after On Create).

So, it sounds like either way I do it I need layout calculated to use in future calculations. It is almost like I need some type of DoEvents after I call AddView so it can draw while still in my function, but everywhere I look acts like Android has no such thing. What does the DoEvents in B4A really do?
 
Last edited:

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Never could get the Tree Global onLayout function to work, but I think I have a Workable solution for my Line Layout version if I require the user to pass a width in. I can then make the Measure() call work and get the measured sizes. With a few more lines of code I can then make it calculate accurate using the passed width. Currently without a width the panel still returns -1, Textviews/EditTexts return minimum size for content (Works for determining Wrap_Content, but not Match_Parent), and ImageViews return 0's (Hopefully that is fixed with an image or set size, but wrapping to content is likely not to work with them). I had to do the same thing in my View Manager class and it isn't ideal...but not much in Android is.
 
Top