CollapsingToolbarLayout crash on 4.4 devices (java.lang.IllegalArgumentException: radius must be > 0)

后端 未结 5 1475
醉梦人生
醉梦人生 2021-02-05 14:54

I have implemented the new style Collapsible Toolbar. I am using the same code as the example (Cheesesquare) demo app - which of course works fine on all devices. I need help

相关标签:
5条回答
  • 2021-02-05 15:31

    There is one thing that stands out to me in your stack errors. It seems in 4.4 and below, your app is trying to draw a gui greater than the actual phone width. The RadialGradient error doesn't make sense to me since, it is supposed to be greater than screen size rather than less than zero.

    /** Create a shader that draws a radial gradient given the center and radius.
        @param centerX  The x-coordinate of the center of the radius
        @param centerY  The y-coordinate of the center of the radius
        @param radius   Must be positive. The radius of the circle for this gradient.
        @param colors   The colors to be distributed between the center and edge of the circle
        @param stops    May be <code>null</code>. Valid values are between <code>0.0f</code> and
                        <code>1.0f</code>. The relative position of each corresponding color in
                        the colors array. If <code>null</code>, colors are distributed evenly
                        between the center and edge of the circle.
        @param tileMode The Shader tiling mode
    */
    public RadialGradient(float centerX, float centerY, float radius,
               @NonNull int colors[], @Nullable float stops[], @NonNull TileMode tileMode) {
        if (radius <= 0) {
            throw new IllegalArgumentException("radius must be > 0");
        }
        if (colors.length < 2) {
            throw new IllegalArgumentException("needs >= 2 number of colors");
        }
        if (stops != null && colors.length != stops.length) {
            throw new IllegalArgumentException("color and position arrays must be of equal length");
        }
        mType = TYPE_COLORS_AND_POSITIONS;
        mX = centerX;
        mY = centerY;
        mRadius = radius;
        mColors = colors;
        mPositions = stops;
        mTileMode = tileMode;
        init(nativeCreate1(centerX, centerY, radius, colors, stops, tileMode.nativeInt));
    }
    

    That's a snippet from the radialgradient class. A way to force the code to run would be to simply edit the radialgradient class if you can do that (Android Studio has a way of doing that).

    My solution to this problem would be to first go through your xml file and start to remove lines of code that are not common/doesn't make sense. Like:

    android:fitsSystemWindows="true"
    

    fitSystemWindow Multiple Calls

    Also another thing your should avoid is the weightsum and weight tags for Linear Layout. And try making your parent widget a RelativeLayout.

    EDIT:

    After thinking for a while (just 2 mins) I realized the error could just be due to the styles.xml implementation. Are you sure you copied all the folders you saw in the git repo?

    Sometimes when this errors occur, it's usually because of the values-v21 folder not been added with the appropriate styles.xml.

    So If you don't have the values-v21 folder, do this: -Create a values-v21 folder under the res folder. -Then create a styles.xml folder under the values-v21 folder and add this code.

    <resources>
    
    <style name="Theme.DesignDemo" parent="Base.Theme.DesignDemo">
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
    

    0 讨论(0)
  • 2021-02-05 15:35

    I have adater with the item main container as cardview and i had the cardCornerRadius set to 0 :

    app:cardCornerRadius="0dp"
    

    I changed it to :

    app:cardCornerRadius="1dp"
    

    It worked perfectly now.

    I also tested with and without android:fitsSystemWindows="true" , it did NOT change anything for me.

    0 讨论(0)
  • 2021-02-05 15:40

    For future people, I found my issue - the Style I was using had the same name as another Style used by the framework (CardView).

    I changed the name of this in my styles.xml file to something else (MyCardView) and the crash went away.

    In hindsight, I was causing a regression by using that common name.

    0 讨论(0)
  • 2021-02-05 15:46

    I had the same issue and I found that I had app:cardCornerRadius="0dp" on cardview attributes. When I removed it then my problem solved. Hope this helps

    0 讨论(0)
  • 2021-02-05 15:53

    According to CardView's Doc:

    Before L, CardView adds padding to its content and draws shadows to that area. This padding amount is equal to maxCardElevation + (1 - cos45) * cornerRadius on the sides and maxCardElevation * 1.5 + (1 - cos45) * cornerRadius on top and bottom.

    Since padding is used to offset content for shadows, you cannot set padding on CardView. Instead, you can use content padding attributes in XML or setContentPadding(int, int, int, int) in code to set the padding between the edges of the Card and children of CardView.

    Check all wrap-parent values. I think some where in your code is changing this padding behavior which caused mCornerRadius < 0 in buildShadowCorners method in RoundRectDrawableWithShadow.java.

    These links maybe help:

    width and height must be > 0 error

    Android CardView padding and minHeight

    CardView inside RecyclerView has extra margins

    0 讨论(0)
提交回复
热议问题