欢迎光临
我们一直在努力

Android 警告信息:Unexpected implicit cast to AppCompatSeekBar: layout tag was SeekBar

<SeekBar
android:id="@+id/seekbar_width"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="2"
android:max="50"
android:progress="10" />

AppCompatSeekBar seekBarWidth = findViewById(R.id.seekbar_width);

  • 在 Android 开发中,上述代码出现如下警告信息

Unexpected implicit cast to AppCompatSeekBar: layout tag was SeekBar

# 解读

意外的隐式转换为 AppCompatSeekBar,布局标签是 SeekBar

问题原因
  • 布局文件中使用的是 SeekBar 控件,代码中则试图将其转换为 AppCompatSeekBar 控件

  • 这个代码在运行时确实不会报错,AppCompatSeekBar 继承自 SeekBar,可以成功向下转型

  • public class AppCompatSeekBar extends SeekBar {
    ...
    }

    处理策略
  • 如果不介意警告,可以保持现状

  • 或者,可以使用 @SuppressWarnings 注解抑制警告

  • @SuppressWarnings("all")
    AppCompatSeekBar seekBarWidth = findViewById(R.id.seekbar_width);

  • 或者,统一类型声明
  • // 统一写成 SeekBar

    SeekBar seekBarWidth = findViewById(R.id.seekbar_width);

    <!– 或者,统一写成 AppCompatSeekBar –>

    <androidx.appcompat.widget.AppCompatSeekBar
    android:id="@+id/seekbar_width"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="5dp"
    android:layout_weight="2"
    android:max="50"
    android:progress="10" />

    赞(0)
    未经允许不得转载:171主机测评 » Android 警告信息:Unexpected implicit cast to AppCompatSeekBar: layout tag was SeekBar
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址