Android Studio和gradle版本升级后,奇怪的布局通胀崩溃

时间:2018-05-16 08:49:23

标签: java android gradle android-gradle android-studio-3.0

我最近升级了我的Android Studio和Gradle版本(请参阅下面的确切版本和上下文)。升级后,应用程序立即崩溃(100%的时间),同时尝试使用以下根本原因对布局进行充气:

 Caused by: java.lang.NumberFormatException: For input string: "3.5"
    at java.lang.Integer.parseInt(Integer.java:608)
    at com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:133)
    at android.content.res.TypedArray.getInt(TypedArray.java:375)
    at com.nirhart.parallaxscroll.views.ParallaxScrollView.init(ParallaxScrollView.java:45)
    at com.nirhart.parallaxscroll.views.ParallaxScrollView.<init>(ParallaxScrollView.java:28)
    at net.chinookbook.android.widget.CBParallaxScrollView.<init>(CBParallaxScrollView.java:19)

无法膨胀的小部件在布局xml中显示如下:

<net.chinookbook.android.widget.CBParallaxScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:fillViewport="true"
    app:parallax_factor="3.5">

可设置的声明是

<declare-styleable name="ParallaxScroll">
    <attr name="parallax_factor" format="float" />
    <attr name="alpha_factor" format="float" />
    <attr name="inner_parallax_factor" format="float" />
    <attr name="parallax_views_num" format="integer" />
    <attr name="circular_parallax" format="boolean" />
</declare-styleable>

使用以下生成的资源:

public static final class styleable {
    public static final int[] ParallaxScroll = { 0x7f040028, 0x7f04007c, 0x7f040101, 0x7f040172, 0x7f040173 };
    public static final int ParallaxScroll_alpha_factor = 0;
    public static final int ParallaxScroll_circular_parallax = 1;
    public static final int ParallaxScroll_inner_parallax_factor = 2;
    public static final int ParallaxScroll_parallax_factor = 3;
    public static final int ParallaxScroll_parallax_views_num = 4;
}

ParallaxScrollView.init()方法是这样的:

protected void init(Context context, AttributeSet attrs) {
    TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.ParallaxScroll);
    this.parallaxFactor = typeArray.getFloat(R.styleable.ParallaxScroll_parallax_factor, DEFAULT_PARALLAX_FACTOR);
    this.alphaFactor = typeArray.getFloat(R.styleable.ParallaxScroll_alpha_factor, DEFAULT_ALPHA_FACTOR);
    this.innerParallaxFactor = typeArray.getFloat(R.styleable.ParallaxScroll_inner_parallax_factor, DEFAULT_INNER_PARALLAX_FACTOR);
    this.numOfParallaxViews = typeArray.getInt(R.styleable.ParallaxScroll_parallax_views_num, DEFAULT_PARALLAX_VIEWS);
    typeArray.recycle();
}

现在,对于在调试代码时观察到的奇怪部分:在行处停止调试器:

typeArray.getFloat(R.styleable.ParallaxScroll_parallax_factor, DEFAULT_PARALLAX_FACTOR);

我可以看到该方法即将被调用(这是调试器评估的) - 看起来是正确的: typeArray.getFloat(3,1.9F)。但接着我介入方法

public float getFloat(@StyleableRes int index, float defValue)

令我惊讶的是, index = 0 defValue = 1.9 。该应用程序在此处不会崩溃,它在此行中也是如此:

typeArray.getInt(R.styleable.ParallaxScroll_parallax_views_num, DEFAULT_PARALLAX_VIEWS)

应该使用 typeArray.getInt(4,1)调用,但是当我进入方法时

public int getInt(@StyleableRes int index, int defValue)

再次,令我惊讶的是参数值是 index = 3 defValue = 1 。这次,应用程序将崩溃,因为索引为3的可样式资源是 parallax_factor ,它被定义为3.5并且不能作为int读取。

所以,我的问题是,如何使用某些(主要)值调用方法,然后实际上用其他值调用。我错过了什么? 注意: ParallaxScrollView是从外部库导入的

=============================================== =
环境:

Android Studio: 3.1.2
Gradle: 3.1.2 (从2.3.3升级)
Gradle包装: 4.4 (从3.3升级)

compileSdkVersion 27
buildToolsVersion 27.0.3
minSdkVersion 16
targetSdkVersion 27
multiDexEnabled true
com.android.support:multidex: 1.0.3 (从1.0.1升级)
com.github.nirhart:parallaxscroll:的 1.0

0 个答案:

没有答案