Unable to inflate floating action button error

558 views Asked by At

I am unable to start my Activity, the app force closes, an error regarding unable to inflate the FAB, I have read the other answers provided for some question, but most of them mentioned to change the background tint attribute from their xml, and others mentioned to add the compat attribute to your activity, I have already done them, yet still getting the error. Could any one please figure out, where I am doing wrong.

Error Log

Unable to start activity ComponentInfo{com.fayaz.pix/com.fayaz.pix.MainActivity}: android.view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class android.support.design.widget.FloatingActionButton

activity_main.xml

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

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>

Gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
    applicationId "com.fayaz.pix"
    minSdkVersion 19
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.google.firebase:firebase-core:9.6.0'
compile 'com.firebaseui:firebase-ui-database:0.6.1'
compile 'com.firebaseui:firebase-ui-storage:0.6.1'
compile 'com.android.support:recyclerview-v7:23.1.0'
compile 'com.android.support:cardview-v7:23.1.0'
}
apply plugin: 'com.google.gms.google-services'

Activity

public class MainActivity extends AppCompatActivity {

private RecyclerView mPostRV;
private FirebaseRecyclerAdapter<Post, PostViewHolder> mPostAdapter;
private DatabaseReference mPostRef;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    initialiseScreen();

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            sendPosttoFirebase();
        }
    });
}

private void sendPosttoFirebase() {
    Post post = new Post();
    String UID = Utils.getUID();

    post.setUID(UID);
    post.setNumLikes(0);
    post.setImageUrl("https://static.pexels.com/photos/36487/above-adventure-aerial-air.jpg");

    mPostRef.child(UID).setValue(post);
}

private void initialiseScreen() {
    mPostRV = (RecyclerView)findViewById(R.id.post_rv);
    mPostRV.setLayoutManager(new LinearLayoutManager(MainActivity.this));
    mPostRef = FirebaseDatabase.getInstance().getReference(Constants.POSTS);
    setupAdapter();
    mPostRV.setAdapter(mPostAdapter);
}

private void setupAdapter() {
    mPostAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(Post.class, R.layout.item_layout,PostViewHolder.class, mPostRef) {
        @Override
        protected void populateViewHolder(PostViewHolder viewHolder, final Post model, int position) {
            StorageReference storageReference = FirebaseStorage.getInstance().getReferenceFromUrl(model.getImageUrl());
            Glide.with(MainActivity.this)
                    .using(new FirebaseImageLoader())
                    .load(storageReference)
                    .into(viewHolder.postIV);
            viewHolder.setNumLikes(model.getNumLikes());
            viewHolder.postLikes.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    updateNumLikes(model.getUID());
                }
            });
        }
    };
}

private void updateNumLikes(String uid) {
    mPostRef.child(uid).child(Constants.NUM_LIKES).runTransaction(new Transaction.Handler() {
        @Override
        public Transaction.Result doTransaction(MutableData mutableData) {
            long num = (long) mutableData.getValue();
            num++;
            mutableData.setValue(num);
            return Transaction.success(mutableData);
        }

        @Override
        public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {

        }
    });
}

public static class PostViewHolder extends RecyclerView.ViewHolder {
    public ImageView postIV, postLikes;
    public TextView numLikes;

    public PostViewHolder(View itemView) {
        super(itemView);

        postIV = (ImageView)itemView.findViewById(R.id.post_iv);
        postLikes = (ImageView)itemView.findViewById(R.id.likes_iv);
        numLikes =  (TextView)itemView.findViewById(R.id.num_likes_tv);

    }

    public void setNumLikes(long num){
        numLikes.setText(String.valueOf(num));`enter code here`
    }
}
}

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fayaz.pix">
<uses-permission android:name="android.permission.INTERNET"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>
1

There are 1 answers

0
Mohammad Reza Majid Pour On

i dont know if it works or not but did u tried to surround all your xml code with linear layout or relative layout?