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

后端 未结 5 1493
醉梦人生
醉梦人生 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 null. Valid values are between 0.0f and
                        1.0f. The relative position of each corresponding color in
                        the colors array. If null, 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.

    
    
    
    

提交回复
热议问题