java.lang.IllegalArgumentException:Rect应该与子节点相交

时间:2016-09-16 16:48:50

标签: android android-layout dalvik

在Android Studio中启动新项目并选择Tabbed Activity后,在构建项目后,我在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)

这个例外意味着什么,以及如何解决它?这是一个全新的项目,所以我做了任何改变。

6 个答案:

答案 0 :(得分:20)

将新的appcompat版本更新为24.2.1后,我遇到了同样的错误, 尝试将版本降低到24.1.1甚至是稳定的23版本。

答案 1 :(得分:9)

在我的情况下,问题是由于FloatingActionButton.Behavior引起的。

这里是协调器布局中的代码

  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.");
        }
    }

这里是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;
    }

正如您所看到的,getInsetDodgeRect返回true,并且由于某种原因,rect不相交。这导致了问题。

解决方法。

我可以修复它只是扩展行为并覆盖方法getInsetDodgeRect以返回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;
}
...

答案 2 :(得分:7)

这是支持库24.2.1see here中引入的错误。

已知的解决方法:

  • 降级到其他支持库版本

答案 3 :(得分:3)

该错误已在25.1.0中修复

答案 4 :(得分:1)

当使用基于旧版appcompat的布局时,会出现问题,查看布局xml文件并进行编辑。擦除工具:上下文属性并解决问题。

答案 5 :(得分:0)

我遇到了同样的错误但是有些如何卸载应用并重新运行它有助于解决它