java.lang.IllegalArgumentException: Rect should intersect with child's bounds

匿名 (未验证) 提交于 2019-12-03 02:16:02

问题:

In Android Studio after starting a new project, and selecting a Tabbed Activity, after the project is build, I get this error in the Android Monitor:

E/AndroidRuntime: FATAL EXCEPTION: main  Process: com.example.app, PID: 23581  java.lang.IllegalArgumentException: Rect should intersect with child's bounds.      at android.support.design.widget.CoordinatorLayout.offsetChildByInset(CoordinatorLayout.java:1319)      at android.support.design.widget.CoordinatorLayout.onChildViewsChanged(CoordinatorLayout.java:1257)      at android.support.design.widget.CoordinatorLayout$OnPreDrawListener.onPreDraw(CoordinatorLayout.java:1805)      at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1867)      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)      at android.view.Choreographer.doCallbacks(Choreographer.java:574)      at android.view.Choreographer.doFrame(Choreographer.java:544)      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)      at android.os.Handler.handleCallback(Handler.java:733)      at android.os.Handler.dispatchMessage(Handler.java:95)      at android.os.Looper.loop(Looper.java:136)      at android.app.ActivityThread.main(ActivityThread.java:5001)      at java.lang.reflect.Method.invokeNative(Native Method)      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(Native Method)

What does this exception mean, and how to fix it? It is a completely new project, so I have not made any change.

回答1:

After updating the new appcompat version to 24.2.1 i had the same bug, Try to lower the version to 24.1.1 or even to a stable 23 version.



回答2:

In my case the problem was caused because of FloatingActionButton.Behavior.

Here the code inside coordinator layout

  if (behavior != null && behavior.getInsetDodgeRect(this, child, rect)) {         // Make sure that it intersects the views bounds         if (!rect.intersect(child.getLeft(), child.getTop(),                 child.getRight(), child.getBottom())) {             throw new IllegalArgumentException("Rect should intersect with child's bounds.");         }     }

And here the code inside of FloatingActionButton.Behavior

    @Override     public boolean getInsetDodgeRect(@NonNull CoordinatorLayout parent,             @NonNull FloatingActionButton child, @NonNull Rect rect) {         // Since we offset so that any internal shadow padding isn't shown, we need to make         // sure that the shadow isn't used for any dodge inset calculations         final Rect shadowPadding = child.mShadowPadding;         rect.set(child.getLeft() + shadowPadding.left,                 child.getTop() + shadowPadding.top,                 child.getRight() - shadowPadding.right,                 child.getBottom() - shadowPadding.bottom);         return true;     }

As you can see getInsetDodgeRect was returning true and by some reason rect was not intersecting. This causes the problem.

The workaround.

I could fix it just extending the behavior and overwriting the method getInsetDodgeRect to return false;

public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior { ... @Override public boolean getInsetDodgeRect(@NonNull CoordinatorLayout parent, @NonNull FloatingActionButton child, @NonNull Rect rect) {     super.getInsetDodgeRect(parent, child, rect);     return false; } ...


回答3:

It's a bug introduced in support library 24.2.1, see here.

Known workarounds:

  • Downgrade to a different support library version


回答4:

The bug have been fixed in 25.1.0



回答5:

The problem is present when used a layout based on a old version appcompat, look the layout xml file and edit it. Erase tools:context atribut and the problem is solved.



回答6:

i was getting the same error but some how uninstalling the app and re running it again helped solve it



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!