问题
Error: Invalid drawable added to LayerDrawable! Drawable already belongs to another owner but does not expose a constant state.
I suddenly noticed this error today, and I'm not sure if it was because I just updated my testing device to Android 8.0. The error message clearly states there's something wrong with setting the ripple effect on the floating action button, and indeed there is no ripple effect when the button is pressed. However, I'm not sure what is causing this problem. Actually, the exact same bug is thrown twice in a row. Any help would be much appreciated! The rest of the app still runs normally, but the bug is really bothering me.
p.s. minSdkVersion is 22, targetSdkVersion and compiledSdkVersion are 27
In MyActivity, line 117 is the data binding and setting the content view.
ActivityMyBinding binding = DataBindingUtil.setContentView(
this, R.layout.activity_my);
Here is the full stacktrace:
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$001(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)
回答1:
Changing app:srcCompat="@android:drawable/..."
to android:src="@android:drawable/..."
fixed it for me.
Strange... whoever posted an answer before pointed me to the solution, but they deleted their answer. =/
They mentioned how a Drawable has a state, and if you assign it to more than one Floating Action Button, then there will be a problem keeping track of the Drawable's state. Apparently with a recent update, this problem was fixed. This led me to realize that I was setting the FAB's source with app:srcCompat rather than android:src.
回答2:
I needed to use app:backgroundTint
instead of android:backgroundTint
回答3:
if you have a Fab in the activity xml, keep the below code and delete android:backgroundTint="@color/colorPrimary"
This solved the issue.
<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" />
来源:https://stackoverflow.com/questions/49620810/drawable-already-belongs-to-another-owner-but-does-not-expose-a-constant-state