Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IllegalArgumentException in AutoCompleteTextView showDropDown

Tags:

android

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

like image 503
user2872856 Avatar asked Mar 21 '18 12:03

user2872856


2 Answers

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?

like image 113
Nonos Avatar answered Oct 10 '22 23:10

Nonos


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();
    }
like image 22
user2872856 Avatar answered Oct 11 '22 00:10

user2872856