当表达式不与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)
已解决,附加说明:
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;
}