Drawable 已经属于另一个所有者,但不公开常量状态
Drawable already belongs to another owner but does not expose a constant state
错误:向 LayerDrawable 添加了无效的可绘制对象! Drawable 已经属于另一个所有者,但不公开常量状态。
今天突然发现这个错误,不知道是不是因为我刚把测试机升级到Android8.0。错误消息清楚地指出在浮动操作按钮上设置波纹效果有问题,按下按钮时确实没有波纹效果。但是,我不确定是什么导致了这个问题。实际上,完全相同的错误连续抛出两次。任何帮助将非常感激!应用程序的其余部分仍然可以正常运行,但该错误确实困扰着我。
p.s。 minSdkVersion为22,targetSdkVersion和compiledSdkVersion为27
在MyActivity中,第117行是数据绑定和设置内容视图。
ActivityMyBinding binding = DataBindingUtil.setContentView(
this, R.layout.activity_my);
这是完整的堆栈跟踪:
W/LayerDrawable: Invalid drawable added to LayerDrawable! Drawable already belongs to another owner but does not expose a constant state.
java.lang.RuntimeException
at android.graphics.drawable.LayerDrawable$ChildDrawable.<init>(LayerDrawable.java:1855)
at android.graphics.drawable.LayerDrawable$LayerState.<init>(LayerDrawable.java:1975)
at android.graphics.drawable.LayerDrawable.createConstantState(LayerDrawable.java:168)
at android.graphics.drawable.LayerDrawable.mutate(LayerDrawable.java:1779)
at android.graphics.drawable.LayerDrawable.mutate(LayerDrawable.java:1785)
at android.graphics.drawable.RippleDrawable.mutate(RippleDrawable.java:997)
at android.view.View.applyBackgroundTint(View.java:21809)
at android.view.View.setBackgroundDrawable(View.java:21680)
at android.support.design.widget.FloatingActionButton.access[=11=]1(FloatingActionButton.java:68)
at android.support.design.widget.FloatingActionButton$ShadowDelegateImpl.setBackgroundDrawable(FloatingActionButton.java:824)
at android.support.design.widget.FloatingActionButtonLollipop.setBackgroundDrawable(FloatingActionButtonLollipop.java:73)
at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:179)
at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:151)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:276)
at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:261)
at com.generica.genericb.genericc.MyActivity.onCreate(MyActivity.java:117)
at android.app.Activity.performCreate(Activity.java:7174)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
将 app:srcCompat="@android:drawable/..."
更改为 android:src="@android:drawable/..."
为我修复了它。
奇怪...之前发布答案的人向我指出了解决方案,但他们删除了他们的答案。 =/
他们提到 Drawable 如何具有状态,如果您将其分配给多个 Floating Action Button,那么跟踪 Drawable 的状态就会出现问题。显然在最近的更新中,这个问题得到了解决。这让我意识到我正在使用 app:srcCompat
而不是 android:src
.
设置 FAB 的源
我需要使用 app:backgroundTint
而不是 android:backgroundTint
如果您在 activity xml 中有 Fab,请保留以下代码并删除 android:backgroundTint="@color/colorPrimary"
这解决了问题。
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/list_fab_margin"
android:layout_marginTop="@dimen/list_fab_margin"
android:layout_marginEnd="@dimen/list_fab_margin"
android:layout_marginBottom="@dimen/list_fab_margin"
app:backgroundTint="@color/colorPrimary"
app:fabSize="mini"
app:srcCompat="@drawable/ic_edit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="41dp"
tools:layout_editor_absoluteY="16dp" />
错误:向 LayerDrawable 添加了无效的可绘制对象! Drawable 已经属于另一个所有者,但不公开常量状态。
今天突然发现这个错误,不知道是不是因为我刚把测试机升级到Android8.0。错误消息清楚地指出在浮动操作按钮上设置波纹效果有问题,按下按钮时确实没有波纹效果。但是,我不确定是什么导致了这个问题。实际上,完全相同的错误连续抛出两次。任何帮助将非常感激!应用程序的其余部分仍然可以正常运行,但该错误确实困扰着我。
p.s。 minSdkVersion为22,targetSdkVersion和compiledSdkVersion为27
在MyActivity中,第117行是数据绑定和设置内容视图。
ActivityMyBinding binding = DataBindingUtil.setContentView(
this, R.layout.activity_my);
这是完整的堆栈跟踪:
W/LayerDrawable: Invalid drawable added to LayerDrawable! Drawable already belongs to another owner but does not expose a constant state.
java.lang.RuntimeException
at android.graphics.drawable.LayerDrawable$ChildDrawable.<init>(LayerDrawable.java:1855)
at android.graphics.drawable.LayerDrawable$LayerState.<init>(LayerDrawable.java:1975)
at android.graphics.drawable.LayerDrawable.createConstantState(LayerDrawable.java:168)
at android.graphics.drawable.LayerDrawable.mutate(LayerDrawable.java:1779)
at android.graphics.drawable.LayerDrawable.mutate(LayerDrawable.java:1785)
at android.graphics.drawable.RippleDrawable.mutate(RippleDrawable.java:997)
at android.view.View.applyBackgroundTint(View.java:21809)
at android.view.View.setBackgroundDrawable(View.java:21680)
at android.support.design.widget.FloatingActionButton.access[=11=]1(FloatingActionButton.java:68)
at android.support.design.widget.FloatingActionButton$ShadowDelegateImpl.setBackgroundDrawable(FloatingActionButton.java:824)
at android.support.design.widget.FloatingActionButtonLollipop.setBackgroundDrawable(FloatingActionButtonLollipop.java:73)
at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:179)
at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:151)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:276)
at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:261)
at com.generica.genericb.genericc.MyActivity.onCreate(MyActivity.java:117)
at android.app.Activity.performCreate(Activity.java:7174)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
将 app:srcCompat="@android:drawable/..."
更改为 android:src="@android:drawable/..."
为我修复了它。
奇怪...之前发布答案的人向我指出了解决方案,但他们删除了他们的答案。 =/
他们提到 Drawable 如何具有状态,如果您将其分配给多个 Floating Action Button,那么跟踪 Drawable 的状态就会出现问题。显然在最近的更新中,这个问题得到了解决。这让我意识到我正在使用 app:srcCompat
而不是 android:src
.
我需要使用 app:backgroundTint
而不是 android:backgroundTint
如果您在 activity xml 中有 Fab,请保留以下代码并删除 android:backgroundTint="@color/colorPrimary"
这解决了问题。
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/list_fab_margin"
android:layout_marginTop="@dimen/list_fab_margin"
android:layout_marginEnd="@dimen/list_fab_margin"
android:layout_marginBottom="@dimen/list_fab_margin"
app:backgroundTint="@color/colorPrimary"
app:fabSize="mini"
app:srcCompat="@drawable/ic_edit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="41dp"
tools:layout_editor_absoluteY="16dp" />