android - 尝试在空对象引用上调用虚拟方法 'android.graphics.Rect android.graphics.drawable.Drawable.getBounds()'
问题描述
我有以下代码:
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 报告说:
尝试在空对象引用上调用虚拟方法 'android.graphics.Rect android.graphics.drawable.Drawable.getBounds()'
品牌:小米,型号:Redmi Note 6 Pro,Android:9。
这里有没有人知道什么会导致drawable“消失”或被视为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" />
解决方案
在您的 xml 中,它说 drawableEnd 而在您的代码中,您假设它是右侧的可绘制对象。我感觉崩溃发生在使用 RTL 语言的设备上(意思是,drawable 将在左侧)。
推荐阅读
- javascript - 将 HTML 电子邮件安全地插入页面
- unity3d - 需要以特定方式旋转物体 - 卡住了万向节锁?
- sonarqube - 长期分支的 Sonarqube 分支结果未按预期显示
- docker - 在 docker 中构建 golang 项目 - 在 $GOPATH 或 $GOROOT 中找不到包
- apache-kafka - Kafka 的提交问题
- jquery - jQuery 用 a href 包装标签
- sas - 多次调用宏
- java - JAX-RS 路径匹配
- java - 使用 JDBC 和 MySQL 插入 1000 条具有关系的记录并忽略重复项
- r - 在 R 中逐行累积