You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级Android Studio与Gradle后布局加载崩溃问题求助

升级Android Studio与Gradle后布局加载崩溃:参数索引错乱问题

最近我把Android Studio和Gradle版本都升级了(具体版本见下文环境信息),结果升级后App加载布局时直接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);时,按逻辑参数应该是(3,1.9F),但进入方法后实际传入的却是index=0,defValue=1.9;
  • 调用typeArray.getInt(R.styleable.ParallaxScroll_parallax_views_num, DEFAULT_PARALLAX_VIEWS)时,预期参数是(4,1),但实际传入的却是index=3,defValue=1;
    这种参数索引的错乱,导致代码错误地把parallax_factor的浮点值当成int类型读取,直接触发了崩溃。

想请教一下,为什么方法调用的参数预期值和实际传入值会出现这种不符的情况?另外说明一下,ParallaxScrollView这个控件来自外部库com.github.nirhart:parallaxscroll:1.0。

环境信息

  • Android Studio: 3.1.2
  • Gradle: 3.1.2(从2.3.3升级)
  • Gradle wrapper: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升级)

内容的提问来源于stack exchange,提问作者Cosmin Radu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:59:54