Android支持库浮动操作按钮异常

时间:2015-10-05 20:44:34

标签: android android-support-library floating-action-button

我遇到关于android.support.design.widget.FloatingActionButton的奇怪行为。我已经google了一下,但没有提到这个组件的这种错误。

只有在我启用了android StrictMode的情况下才会发生这种情况。我得到以下异常:

CoordinatorLayout﹕ Default behavior class android.support.design.widget.FloatingActionButton$a could not be instantiated. Did you forget a default constructor?
java.lang.InstantiationException: can't instantiate class android.support.design.widget.FloatingActionButton$a; no empty constructor
        at java.lang.Class.newInstanceImpl(Native Method)
        at java.lang.Class.newInstance(Class.java:1130)
        at android.support.design.widget.CoordinatorLayout.a(CoordinatorLayout.java:503)
        at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:579)
        at android.view.View.measure(View.java:16831)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16831)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:16831)
        at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:868)
        at android.view.View.measure(View.java:16831)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16831)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:16831)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
        at android.view.View.measure(View.java:16831)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:16831)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16831)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:16831)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2586)
        at android.view.View.measure(View.java:16831)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2189)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1352)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1535)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
        at android.view.Choreographer.doCallbacks(Choreographer.java:591)
        at android.view.Choreographer.doFrame(Choreographer.java:561)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeN

然而,这不会使我的应用程序崩溃或抛出任何可见错误。每次创建具有FAB的活动时抛出它,但FAB呈现并且完美地运行。如果我点击它,它会执行onClickListener所说的应该做的事情。

我的build.gradle

是我的Android支持库依赖项
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'

这就是我在xml布局中使用FAB的方式:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <fragment
        android:id="@+id/home_toolbar_wallet"
        android:name="my.fragment.class"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    (...)
</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/home_floating_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_add_white_18dp"
    android:layout_margin="@dimen/action_button_margin"
    app:borderWidth="0dp"
    app:layout_anchor="@id/home_toolbar_wallet"
    app:layout_anchorGravity="bottom|right|end"/>

如果您想知道,这就是我启用StrictMode的方式:

StrictMode.ThreadPolicy.Builder builder = new StrictMode.ThreadPolicy.Builder builder = new StrictMode.ThreadPolicy.Builder()
    .detectAll()
    .penaltyLog();
if (Utils.hasHoneycomb()) {
    builder.penaltyFlashScreen();
}
StrictMode.setThreadPolicy(builder.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
    .penaltyLog()
    .build());

有人遇到过这个吗?

1 个答案:

答案 0 :(得分:5)

这可能是由于程序混淆了你的代码。 您是否尝试将此规则添加到您的proguard规则中?

-keepclassmembers class android.support.design.widget.FloatingActionButton$Behavior { public <init>(); }