8

I received this crash report in Google Play Console which I myself never experience.

java.lang.IllegalArgumentException: 
  at android.widget.ListPopupWindow.setHeight (ListPopupWindow.java:541)
  at android.widget.AutoCompleteTextView.setDropDownHeight (AutoCompleteTextView.java:414)
  at .MyEditText.showDropDown (MyEditText.java:44)
  at android.widget.AutoCompleteTextView.updateDropDownForFilter (AutoCompleteTextView.java:1086)
  at android.widget.AutoCompleteTextView.onFilterComplete (AutoCompleteTextView.java:1068)
  at android.widget.Filter$ResultsHandler.handleMessage (Filter.java:285)
  at android.os.Handler.dispatchMessage (Handler.java:105)
  at android.os.Looper.loop (Looper.java:172)
  at android.app.ActivityThread.main (ActivityThread.java:6637)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:240)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:767)

I am using this showDropDown method to leave the space of 50 dp from the bottom of screen so that the drop down will not cover my Admob ads on the bottom.

public void showDropDown() {
    Rect displayFrame = new Rect();
    getWindowVisibleDisplayFrame(displayFrame);

    int[] locationOnScreen = new int[2];
    getLocationOnScreen(locationOnScreen);

    int bottom = locationOnScreen[1] + getHeight();
    int availableHeightBelow = displayFrame.bottom - bottom;
    Resources r = getResources();
    int bannerHeight = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, r.getDisplayMetrics()));
    int downHeight = availableHeightBelow - bannerHeight;
    setDropDownHeight(downHeight);

    super.showDropDown();
}

From Google Play Console, this crash only affects Mi A1 and Mate 10 Pro running Android 8.0. I do not experience this crash on emulator running Android 8.0.

This is the desired effect:

enter image description here

user2872856
  • 2,003
  • 2
  • 21
  • 54

2 Answers2

7

It looks like the IllegalArgumentException is thrown here. If you track earlier versions of Android (N and earlier) that defensive code does not exist. Based on your computations, the height could be negative. I think you'd need a different way to achieve the desired result. How does your layout look like?

Nonos
  • 2,450
  • 2
  • 23
  • 34
  • I have added a screenshot. I just want to leave a space of 50 dp from the bottom of screen. – user2872856 Mar 28 '18 at 02:19
  • Looking at your computations, if availableHeightBelow is less than 50 dp you would get this exception (you could somehow try to reproduce it by creating a really long list of suggestion in the autocomplete?) – Nonos Mar 29 '18 at 20:04
  • 1
    I feel the computation is incorrect. You're setting the height of the drop down to height below - 50dp, I think you need to set it to the whole height available in the window - (height of the keyboard + 50dp) – Nonos Mar 29 '18 at 20:14
4

For the time being, I added code to check whether downHeight > 0 to prevent this crash.

public void showDropDown() {
        Rect displayFrame = new Rect();
        getWindowVisibleDisplayFrame(displayFrame);

        int[] locationOnScreen = new int[2];
        getLocationOnScreen(locationOnScreen);

        int bottom = locationOnScreen[1] + getHeight();
        int availableHeightBelow = displayFrame.bottom - bottom;
        Resources r = getResources();
        int bannerHeight = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, r.getDisplayMetrics()));
        int downHeight = availableHeightBelow - bannerHeight;
        if (downHeight > 0) {
            setDropDownHeight(downHeight);
        } else {
            setDropDownHeight(300);
        }

        super.showDropDown();
    }
user2872856
  • 2,003
  • 2
  • 21
  • 54
  • What is the role of `bannerHeight` exactly? Does it just set something like marginBottom for the dropdown list? Can I remove it from my code or it is related to the crash? – Alireza Noorali May 21 '19 at 04:29