Getting error inflating CardView despite of using

2019-08-22 02:55发布

问题:

I have a problem which is wasting my time. I was working on my project very well until yesterday. When I started to work last morning I have faced this problem ;

"Binary XML file line #2: Error inflating class android.support.v7.widget.CardView"

I have suddennly googled and I have seen that some other people faced either. But answers about these problem is not my solution. It hasnt fixed my problem. Pls save my day :)

here is my gradle ;

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:support-compat:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support:animated-vector-drawable:27.1.1'
implementation 'com.android.support:exifinterface:27.1.1'
implementation 'com.android.support:customtabs:27.1.1'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//new Lİbrary
implementation 'com.github.mancj:MaterialSearchBar:0.7.6'
implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
implementation 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'
implementation 'com.cepheuen.elegant-number-button:lib:1.0.2'
implementation 'com.github.jd-alexander:android-flat-button:v1.1'
implementation 'com.stepstone.apprating:app-rating:2.0.0'
implementation 'com.github.andremion:counterfab:1.0.1'
implementation 'uk.co.chrisjenx:calligraphy:2.3.0'
implementation 'io.paperdb:paperdb:2.6'
//slider
implementation 'com.nineoldandroids:library:2.4.0'
implementation 'com.daimajia.slider:library:1.1.5@aar'
implementation 'com.android.support:multidex:1.0.3'
//facebook account kit
implementation 'com.facebook.android:facebook-share:4.33.0'
implementation 'com.rengwuxian.materialedittext:library:2.1.4'
implementation 'com.google.gms:google-services:4.0.1'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.flaviofaria:kenburnsview:1.0.7'
implementation 'com.github.d-max:spots-dialog:1.1@aar'
implementation 'com.google.android.gms:play-services-places:15.0.1'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-auth:16.0.2'
implementation 'com.firebaseui:firebase-ui-auth:3.3.1'
implementation 'com.firebaseui:firebase-ui-database:3.3.1'
implementation 'com.google.firebase:firebase-messaging:17.1.0'}

here is my logcat;

 Process: com.orderfood.teknomerkez.orderfood, PID: 14852
android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.v7.widget.CardView
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.CardView
Caused by: java.lang.reflect.InvocationTargetException
    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.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at com.orderfood.teknomerkez.orderfood.Home$5.onCreateViewHolder(Home.java:277)
    at com.orderfood.teknomerkez.orderfood.Home$5.onCreateViewHolder(Home.java:272)

Here is my xml file;

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    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="200dp"
    app:cardElevation="4dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.flaviofaria.kenburnsview.KenBurnsView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/backround_main_page"
            android:id="@+id/menu_image"
            android:scaleType="centerCrop" />

        <TextView
            android:fontFamily="@font/fondamento"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/menu_name"
            android:text="Menu Name"
            android:textColor="@color/white"
            android:background="#4f0e0d0e"
            android:textSize="20sp"
            android:gravity="center"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>
</android.support.v7.widget.CardView>

here is my viewHolder;

public class MenuViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

public TextView txtMenuName;
public KenBurnsView menu_image;
private ItemClickListener itemClickListener;

public MenuViewHolder(View itemView) {
    super(itemView);
    txtMenuName = itemView.findViewById(R.id.menu_name);
    menu_image = itemView.findViewById(R.id.menu_image);
    itemView.setOnClickListener(this);
}

public void setItemClickListener(ItemClickListener itemClickListener) {
    this.itemClickListener = itemClickListener;
}

@Override
public void onClick(View view) {
itemClickListener.onClick(view,getAdapterPosition(),false);
}

}

here is my adapter;

Query query = FirebaseDatabase.getInstance()
            .getReference().child("Category");
    FirebaseRecyclerOptions<Category> options = new FirebaseRecyclerOptions.Builder<Category>().setQuery(query, Category.class).build();
    adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(options) {
        @NonNull
        @Override
        public MenuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.menu_item, parent, false);
            return new MenuViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(@NonNull MenuViewHolder viewHolder, int position, @NonNull Category model) {
            viewHolder.txtMenuName.setText(model.getName());
            Picasso p = Picasso.get();
            p.load(model.image).into(viewHolder.menu_image);

            final Category clickItem = model;
            viewHolder.setItemClickListener(new ItemClickListener() {
                @Override
                public void onClick(View view, int position, boolean isLongClick) {
                    Toast.makeText(Home.this, "" + clickItem.getName(), Toast.LENGTH_SHORT).show();
                    Intent food_intent = new Intent(Home.this, FoodList.class);
                    //Because categoryId is key , so we just get key of this item
                    food_intent.putExtra(getCategoryId, adapter.getRef(position).getKey());
                    food_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(food_intent);
                }
            });
        }
    };
  adapter.startListening();

回答1:

Remove that starting space in your xml.(Just before <?xml version="1.0" encoding="utf-8"?>) Reason: Xml declaration should precede all document content.