AutoCompleteTextView showDropDown中的IllegalArgumentException

时间:2018-03-21 12:49:33

标签: android

我在Google Play控制台收到了此崩溃报告,我自己从未体验过。

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)

我正在使用此showDropDown方法从屏幕底部留出50 dp的空间,以便下拉菜单不会覆盖底部的Admob广告。

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

从Google Play控制台,此次崩溃只影响运行Android 8.0的Mi A1和Mate 10 Pro。我没有在运行Android 8.0的模拟器上遇到此崩溃。

这是期望的效果:

enter image description here

2 个答案:

答案 0 :(得分:7)

看起来抛出了IllegalArgumentException here。 如果您跟踪早期版本的Android(N及更早版本),那么防御代码就不存在了。 根据您的计算,高度可能是负数。我认为你需要一种不同的方式来达到预期的效果。你的布局如何?

答案 1 :(得分:3)

目前,我添加了代码以检查downHeight > 0是否可以防止此次崩溃。

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