26

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.

goetz
  • 2,018
  • 2
  • 30
  • 33

6 Answers6

20

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.

  • 6
    Yes that's correct, the error occurs after upgrading to version 24.2.1. Go to your app/build.gradle and change to this `'compile 'com.android.support:appcompat-v7:24.2.0` – bmacharia Oct 05 '16 at 07:32
  • 5
    Same problem with 25.0.0... Another bug taken seriously by Google – Yoann Hercouet Oct 26 '16 at 20:47
  • 3
    Yeah I got the same problem with appcompat-v7:25.0.0 – Orcun Sevsay Oct 31 '16 at 12:47
  • if I use com.android.support:appcompat-v7:24.2.0 app crashes for a different reason. It works fine with com.android.support:appcompat-v7:24.1.0. The exception is java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.design.widget.CoordinatorLayout$LayoutParams – kingston Nov 14 '16 at 10:09
9

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;
}
...
jDur
  • 1,481
  • 9
  • 10
  • How do you set the ScrollAwareFABBehavior to the FloatingActionButton? – Bernd Kampl Nov 16 '16 at 10:01
  • 1
  • Using it like this results in a crash on startup with InflateException: Binary XML file: Could not inflate Behavior subclass com.mypackage.etc.ScrollAwareFABBehavior – Bernd Kampl Nov 23 '16 at 12:46
  • I was missing the Constructors, having the following now works: http://pastebin.com/UAzGYyYE – Bernd Kampl Nov 23 '16 at 13:22
  • As of appcompat support version 25.0.1 the bug is still present. The fix you provided works quite well. No need to use an old library. Thank you! – Grux Dec 01 '16 at 19:28
  • Nice fix, it works for me. I had a Fab in a Fragment in ViewPager which was causing the problem. At least that was the only place I could find and reproduce the error. Thank you! – Peter Jan 02 '17 at 23:17
7

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

Known workarounds:

  • Downgrade to a different support library version
darken
  • 807
  • 12
  • 25
  • User Chisko already provided this information, take a look at the first comment on the question. – goetz Sep 22 '16 at 15:49
  • @goetzc Providing it as an answer is better. You can now mark this correct so it shows up as the best way to handle this issue. – Jared Burrows Oct 26 '16 at 16:29
3

The bug have been fixed in 25.1.0

Ammar
  • 1,148
  • 9
  • 15
1

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.

aferrer
  • 11
  • 1
0

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

Ismael ozil
  • 573
  • 5
  • 6