3

I have the following code:

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;
});

While generally it seems to work, I suddenly got a crashlytics report saying:

Attempt to invoke virtual method 'android.graphics.Rect android.graphics.drawable.Drawable.getBounds()' on a null object reference

for Brand: Xiaomi, Model: Redmi Note 6 Pro, Android: 9.

Has anyone here have an idea what can cause the drawable to "disappear" or be considered as null?

XML layout:

    <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" />
Amos
  • 1,321
  • 2
  • 23
  • 44

2 Answers2

5

In your xml it says drawableEnd while in your code your'e assuming it's the drawable on the right. I have a feeling the crash happened on a device with a RTL language (meaning, the drawable will be on the left).

AsafK
  • 2,425
  • 3
  • 32
  • 38
1

In crashlytics, check if the crash happens on the background or foreground. If it's in the background most probably the edit text is destroyed and therefore you get the npe (user got a call before releasing the finger, for example). If not, more details of the flow would help to find out the issue.

Orit Malki
  • 58
  • 6
  • For some reason, it disappeared from Crashlytics. What do you mean by background or foreground? The crash happened to that same user 3-4 times so unless he got a lot of calls... :) There is a login window with email textedit with an icon (drawable) to show more info when clicked on. The user clicked on it (setontouch...) and got NPE. Thanks! – Amos Oct 13 '19 at 08:10
  • Crashlytics dashboard usually specifies whether the crash happened when the app was visible or not. This is inside the specific crash statistics - shows percentage of incidents in background. If it is 100% background that means the crash happens only when app is not visible. If it happens also when the app is visible you should check in what scenarios it may happen. Anyway, a null check should solve it if it happens only when app is not visible. – Orit Malki Oct 13 '19 at 10:52
  • Thanks for the tip, can you tell me where in the crash statistics it shows that? I couldn't find it. – Amos Oct 13 '19 at 18:52
  • OK, found it, 0% background – Amos Oct 14 '19 at 13:42
  • Ok, that makes sense since I saw now the issue was related to RTL configuration that must have happened while the app was visible. Now you also know how to detect when in the user's flow the crash has occurred. – Orit Malki Oct 18 '19 at 16:09