Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当表达式不与onCreateDrawableState-android中的枚举类一起工作时,kotlin_Android_Kotlin_Enums_State_Android Drawable - Fatal编程技术网

当表达式不与onCreateDrawableState-android中的枚举类一起工作时,kotlin

当表达式不与onCreateDrawableState-android中的枚举类一起工作时,kotlin,android,kotlin,enums,state,android-drawable,Android,Kotlin,Enums,State,Android Drawable,我不明白为什么这会在when(colorState)行抛出NPE。如果我删除枚举类并用整数替换值,一切都正常 class ColorChangerButton(context: Context, attrs: AttributeSet) : AppCompatImageButton(context, attrs) { enum class ColorState {ACCENT, STRONG, WEAK} private val stateAccent = intArrayO

我不明白为什么这会在
when(colorState)
行抛出NPE。如果我删除枚举类并用整数替换值,一切都正常

class ColorChangerButton(context: Context, attrs: AttributeSet) : AppCompatImageButton(context, attrs) {

    enum class ColorState {ACCENT, STRONG, WEAK}

    private val stateAccent = intArrayOf(R.attr.state_accent)
    private val stateStrong = intArrayOf(R.attr.state_strong)
    private val stateWeak = intArrayOf(R.attr.state_weak)

    var colorState = ColorState.ACCENT
        set(value) {
            if (field != value) {
                field = value
                refreshDrawableState()
            }
        }

    override fun onCreateDrawableState(extraSpace: Int): IntArray {
        val state = super.onCreateDrawableState(extraSpace + 1)

        when (colorState) {
            ColorState.ACCENT -> View.mergeDrawableStates(state, stateAccent)
            ColorState.STRONG -> View.mergeDrawableStates(state, stateStrong)
            ColorState.WEAK -> View.mergeDrawableStates(state, stateWeak)
        }

        return state
    }
}
堆栈跟踪:

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.test.uiplayground.buttons.ColorChangerButton$ColorState.ordinal()' on a null object reference
    at com.test.uiplayground.buttons.ColorChangerButton.onCreateDrawableState(ColorChangerButton.kt:28)
    at android.view.View.getDrawableState(View.java:15953)
    at android.view.View.setBackgroundDrawable(View.java:16188)
    at androidx.appcompat.widget.AppCompatImageButton.setBackgroundDrawable(AppCompatImageButton.java:122)
    at android.view.View.setBackground(View.java:16125)
    at android.view.View.<init>(View.java:4090)
    at android.widget.ImageView.<init>(ImageView.java:139)
    at android.widget.ImageButton.<init>(ImageButton.java:86)
    at android.widget.ImageButton.<init>(ImageButton.java:82)
    at androidx.appcompat.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:73)
    at androidx.appcompat.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:69)
    at com.test.uiplayground.buttons.ColorChangerButton.<init>(ColorChangerButton.kt:9)
已解决,附加说明:

  • 有关引发NPE的原因,请参见公认的答案
  • 整数部分很容易引起误解。Java将未初始化的变量设置为默认值(除非是本地变量或块变量,否则会出现错误,表示未初始化)。对于整数,该值为0。因此,它在第一次调用时通过了所有3个条件。如果我添加
    0->View.mergeDrawableState(state,stateAccent)
    case,那么我会得到另一个NPE,这一次是因为
    stateAccent
    还没有初始化-这一点现在已经非常合理了

  • 这是为Kotlin源文件生成的Java代码:

      public ColorChangerButton(@NotNull Context context, @NotNull AttributeSet attrs) {
          Intrinsics.checkParameterIsNotNull(context, "context");
          Intrinsics.checkParameterIsNotNull(attrs, "attrs");
          super(context, attrs);                  <---------- super will be called first
          this.stateAccent = new int[0];
          this.stateStrong = new int[0];
          this.stateWeak = new int[0];
          this.colorState = ColorChangerButton.ColorState.ACCENT;
       }
    
    public ColorChangerButton(@NotNull上下文,@NotNull属性集attrs){
    Intrinsics.checkParameterisNotFull(上下文,“上下文”);
    检查参数完整性(attrs,“attrs”);
    
    super(context,attrs);您的colorState字段没有初始值为什么没有?colorState.ACCENT应该是初始值。您能提供异常的完整堆栈跟踪吗?
      public ColorChangerButton(@NotNull Context context, @NotNull AttributeSet attrs) {
          Intrinsics.checkParameterIsNotNull(context, "context");
          Intrinsics.checkParameterIsNotNull(attrs, "attrs");
          super(context, attrs);                  <---------- super will be called first
          this.stateAccent = new int[0];
          this.stateStrong = new int[0];
          this.stateWeak = new int[0];
          this.colorState = ColorChangerButton.ColorState.ACCENT;
       }