尝试在空对象引用上调用虚拟方法 'android.graphics.Rect android.graphics.drawable.Drawable.getBounds()'
Attempt to invoke virtual method 'android.graphics.Rect android.graphics.drawable.Drawable.getBounds()' on a null object reference
我有以下代码:
etEmail.setOnTouchListener((view, motionEvent) -> {
final int DRAWABLE_RIGHT = 2;
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
if(motionEvent.getRawX() >= (etEmail.getRight() - etEmail.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
Utils.getInstance().showPopup("...",MainActivity.this,null);
return true;
}
}
return false;
});
虽然通常它似乎有效,但我突然收到一份 crashlytics 报告说:
Attempt to invoke virtual method 'android.graphics.Rect
android.graphics.drawable.Drawable.getBounds()' on a null object
reference
适用品牌:小米,型号:Redmi Note 6 Pro,Android:9.
这里有没有人知道什么会导致可绘制对象 "disappear" 或被视为 null?
XML布局:
<EditText
android:id="@+id/etEmail"
tools:ignore="Autofill"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/ic_info"
android:paddingEnd="12dp"
android:paddingStart="10dp"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
在 crashlytics 中,检查崩溃是发生在后台还是前台。如果它在后台,很可能编辑文本已被破坏,因此您会收到 npe(例如,用户在松开手指之前接到电话)。
如果不是,流程的更多详细信息将有助于找出问题。
在您的 xml 中它说 drawableEnd 而在您的代码中您假设它是右侧的 drawable。我有一种感觉,崩溃发生在使用 RTL 语言的设备上(意思是,drawable 将在左侧)。
我有以下代码:
etEmail.setOnTouchListener((view, motionEvent) -> {
final int DRAWABLE_RIGHT = 2;
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
if(motionEvent.getRawX() >= (etEmail.getRight() - etEmail.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
Utils.getInstance().showPopup("...",MainActivity.this,null);
return true;
}
}
return false;
});
虽然通常它似乎有效,但我突然收到一份 crashlytics 报告说:
Attempt to invoke virtual method 'android.graphics.Rect android.graphics.drawable.Drawable.getBounds()' on a null object reference
适用品牌:小米,型号:Redmi Note 6 Pro,Android:9.
这里有没有人知道什么会导致可绘制对象 "disappear" 或被视为 null?
XML布局:
<EditText
android:id="@+id/etEmail"
tools:ignore="Autofill"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/ic_info"
android:paddingEnd="12dp"
android:paddingStart="10dp"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
在 crashlytics 中,检查崩溃是发生在后台还是前台。如果它在后台,很可能编辑文本已被破坏,因此您会收到 npe(例如,用户在松开手指之前接到电话)。 如果不是,流程的更多详细信息将有助于找出问题。
在您的 xml 中它说 drawableEnd 而在您的代码中您假设它是右侧的 drawable。我有一种感觉,崩溃发生在使用 RTL 语言的设备上(意思是,drawable 将在左侧)。