另一个 java.lang.IllegalArgumentException: 参数必须是这个视图的后代

Another java.lang.IllegalArgumentException: parameter must be a descendant of this view

用户在某些手机上的我的应用程序中遇到以下异常。我尝试自己重现错误,但不能。我在堆栈溢出中搜索了类似的问题并尝试了他们的解决方案,但 none 似乎有效。

以下是我已经尝试过的答案:

  1. Preventing/catching "IllegalArgumentException: parameter must be a descendant of this view" error
  2. java.lang.IllegalArgumentException: parameter must be a descendant of this view Error
  3. 设置以下属性:android:windowSoftInputMode="stateHidden|adjustPan"

我有一个编辑文本,但它在可扩展列表视图之外。

我正试图找到一个解释,说明为什么会发生这种情况以及可能导致这种情况发生的原因。抱歉,我无法 post 任何代码。

java.lang.IllegalArgumentException: parameter must be a descendant of this view at android.view.ViewGroup.offsetRectBetweenParentAndChild(ViewGroup.java:4568) at android.view.ViewGroup.offsetDescendantRectToMyCoords(ViewGroup.java:4505) at android.view.ViewGroup$ViewLocationHolder.init(ViewGroup.java:6743) at android.view.ViewGroup$ViewLocationHolder.obtain(ViewGroup.java:6680) at android.view.ViewGroup$ChildListForAccessibility.init(ViewGroup.java:6638) at android.view.ViewGroup$ChildListForAccessibility.obtain(ViewGroup.java:6606) at android.view.ViewGroup.addChildrenForAccessibility(ViewGroup.java:1697) at android.view.ViewGroup.onInitializeAccessibilityNodeInfoInternal(ViewGroup.java:2525) at android.view.View.onInitializeAccessibilityNodeInfo(View.java:5213) at android.widget.AdapterView.onInitializeAccessibilityNodeInfo(AdapterView.java:946) at android.widget.AbsListView.onInitializeAccessibilityNodeInfo(AbsListView.java:1449) at android.widget.ListView.onInitializeAccessibilityNodeInfo(ListView.java:3781) at android.widget.ExpandableListView.onInitializeAccessibilityNodeInfo(ExpandableListView.java:1348) at android.view.View.createAccessibilityNodeInfoInternal(View.java:5174) at android.view.View.createAccessibilityNodeInfo(View.java:5161) at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:811) at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:834) at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:834) at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchAccessibilityNodeInfos(AccessibilityInteractionController.java:720) at android.view.AccessibilityInteractionController.findAccessibilityNodeInfoByAccessibilityIdUiThread(AccessibilityInteractionController.java:147) at android.view.AccessibilityInteractionController.access0(AccessibilityInteractionController.java:49) at android.view.AccessibilityInteractionController$PrivateHandler.handleMessage(AccessibilityInteractionController.java:971) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5097) at java.lang.reflect.Method.invokeNative(Method.java) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(NativeStart.java)

问题是由于向两个不同的列表视图添加错误膨胀的 header 引起的。

我使用 listViewA 作为 parent 扩充了视图,并将其也添加到 listViewB。因此:

RelativeLayout listViewHeader = (RelativeLayout) inflater.inflate(R.layout.listviewheader, listViewA, false);

// Set some views

listViewA.addHeaderView(listViewHeader);
listViewB.addHeaderView(listViewHeader);

我通过将上面的内容更改为以下内容来修复它:

RelativeLayout listViewHeaderA = (RelativeLayout) inflater.inflate(R.layout.listviewheader, listViewA, false);
RelativeLayout listViewHeaderB = (RelativeLayout) inflater.inflate(R.layout.listviewheader, listViewB, false);

listViewA.addHeaderView(listViewHeaderA);
listViewB.addHeaderView(listViewHeaderB);

至于重现崩溃,问题发生在 Google Talk Back 开启时。以下是我对这种情况的看法:Google Talk Back 对焦点视图(通过触摸或 auto-focused)进行文本到语音的转换。当它进入一个有多个视图请求焦点的屏幕时,它会根据 hierarchy/order 读取视图。

如果您的布局 (parent) 具有三个视图 (children),Google Talk Back 会检查视图的排列方式,然后相应地读取它们。例如,在三个 textview 水平排列的布局中,Google Talk Back 可能会先读取左侧的 textview,然后是中间的,然后是右侧的。

在我的例子中,我用 listViewA 作为 parent 膨胀 header 视图,并将该视图添加到 listViewAlistViewB。当 listViewB 获得焦点并且 Google Talk Back 试图解释其 children 时,它发现 header 视图不是列表的 child 并抛出例外。如果我在没有 parents (null) 的情况下膨胀 header 视图,也会发生这种情况。

当 Google TalkBack 打开时,我也遇到了同样的错误。在我的例子中,我用布尔值 attachToParent true.So 膨胀了一个视图,使它对我有用。

我在 ScrollView 中附加了滚动侦听器 并解决问题

 lst_payment_info.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if (SCROLL_STATE_TOUCH_SCROLL == scrollState) {
                View currentFocus = getCurrentFocus();
                if (currentFocus != null) {
                    currentFocus.clearFocus();
                }
            }
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        }
    });