首页 > 解决方案 > 尝试在空对象引用上调用虚拟方法 '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" />

标签: androidcompound-drawables

解决方案


在您的 xml 中,它说 drawableEnd 而在您的代码中,您假设它是右侧的可绘制对象。我感觉崩溃发生在使用 RTL 语言的设备上(意思是,drawable 将在左侧)。


推荐阅读