Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FAB throws RuntimeException: Invalid drawable added to LayerDrawable! Drawable already belongs to another owner but does not expose a constant state. #46

Open
Idomo opened this issue Apr 16, 2018 · 2 comments

Comments

@Idomo
Copy link

Idomo commented Apr 16, 2018

I'm having problem with the FAB, it's throws this exception and I can't find the reason...
The app is not crashing, but this error occurs twice, one after the other, and this filling my logcat with bunch of code that "hiding" the important lines.
Logcat:

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:1850)
        at android.graphics.drawable.LayerDrawable$LayerState.<init>(LayerDrawable.java:1967)
        at android.graphics.drawable.LayerDrawable.createConstantState(LayerDrawable.java:168)
        at android.graphics.drawable.LayerDrawable.mutate(LayerDrawable.java:1774)
        at android.graphics.drawable.LayerDrawable.mutate(LayerDrawable.java:1780)
        at android.graphics.drawable.RippleDrawable.mutate(RippleDrawable.java:974)
        at android.view.View.applyBackgroundTint(View.java:20532)
        at android.view.View.setBackgroundDrawable(View.java:20403)
        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 com.idomo.Fab.<init>(Fab.java:17)
        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 com.idomo.RecipesListFragment.onCreateView(RecipesListFragment.java:48)

My FAB class:

package com.idomo;

import android.content.Context;
import android.support.design.widget.FloatingActionButton;
import android.util.AttributeSet;
import android.view.View;

import com.gordonwong.materialsheetfab.AnimatedFab;

public class Fab extends FloatingActionButton implements AnimatedFab{

  public Fab(Context context){
    super(context);
  }

  public Fab(Context context, AttributeSet attrs){
    super(context, attrs); // **The error starts here** (line 17)
  }

  public Fab(Context context, AttributeSet attrs, int defStyleAttr){
    super(context, attrs, defStyleAttr);
  }

  @Override
  public void show(){
    show(0, 0);
  }

  @Override
  public void show(float translationX, float translationY){
    setVisibility(View.VISIBLE);
  }

  @Override
  public void hide(){
    setVisibility(View.INVISIBLE);
  }
}

XML (RecipesListFragment):

<!-- Your FAB implementation -->
  <com.idomo.Fab
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="start|bottom"
    android:layout_margin="@dimen/fab_margin"
    android:backgroundTint="@color/colorPrimary"
    android:src="@drawable/ic_edit" />

  <!-- Overlay that dims the screen -->
  <com.gordonwong.materialsheetfab.DimOverlayFrameLayout
    android:id="@+id/dim_overlay"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

  <!-- Circular reveal container for the sheet -->
  <io.codetail.widget.RevealLinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="start|bottom"
    android:orientation="vertical" />

  <!-- Sheet that contains your items -->
  <android.support.v7.widget.CardView
    android:id="@+id/fab_sheet"
    android:layout_width="200dp"
    android:layout_height="wrap_content">

    <ListView
      android:id="@+id/fabMenu"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
    </ListView>
  </android.support.v7.widget.CardView>

Thanks for helpers,
Ido.

@acoria
Copy link

acoria commented Aug 12, 2018

For anyone who comes across this error too, the problem is
android:backgroundTint="@color/colorPrimary".
It helps to set a colorStateList instead, e.g. like this:
int[][] states = new int[][] {
new int[] { android.R.attr.state_enabled},
new int[] { android.R.attr.state_pressed}
};
int[] colors = new int[] {
ContextCompat.getColor(this, R.color.colorFab),
ContextCompat.getColor(this, R.color.colorFabPressed)
};
ColorStateList fabColorList = new ColorStateList(states, colors);
findViewById(R.id.fab).setBackgroundTintList(fabColorList);

@rikbanerjee
Copy link

if you have a Fab in the view, 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" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants