【问题标题】:Getting error inflating CardView despite of using last version尽管使用了最新版本,但在膨胀 CardView 时出现错误
【发布时间】:2018-12-08 12:44:52
【问题描述】:

我有一个问题正在浪费我的时间。直到昨天,我一直在很好地完成我的项目。当我昨天早上开始工作时,我遇到了这个问题;

“二进制 XML 文件第 2 行:膨胀类 android.support.v7.widget.CardView 时出错”

我突然用谷歌搜索,发现其他人也遇到过。但是关于这些问题的答案不是我的解决方案。它没有解决我的问题。请保存我的一天:)

这是我的毕业典礼;

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'}

这是我的日志;

 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)

这是我的 xml 文件;

 <?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>

这是我的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);
}

}

这是我的适配器;

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();

【问题讨论】:

  • 你能发布viewholder类吗?可能是不匹配导致此
  • 添加先生,请检查
  • 您的所有代码在这里看起来都很好。我认为这不是 cardView 的问题。但是您是否尝试过将 cardView 更改为其他布局?
  • 我想是的,先生,我已经用 FrameLayout 进行了更改,但在从CardView 27.1.1 到 27.1.0,似乎已经解决了,但是毕加索又出现了新问题。

标签: android exception dependencies android-inflate cardview


【解决方案1】:

删除 xml 中的起始空间。(就在 &lt;?xml version="1.0" encoding="utf-8"?&gt; 之前) 原因:xml声明应该在所有文档内容之前。

【讨论】:

  • 如果你的意思是 "" 这句话我做过,但遇到了同样的问题。
  • @jDeveloper,在您发布的 xml 代码中。我可以在 xml 声明之前看到一个空白区域,您确定在代码中删除了它吗?并尝试重建项目?
  • 是的,我已经尝试过了。现在我在我的 xml 中编辑了没有空间。我再次重建并尝试测试,不幸的是没有工作
猜你喜欢
  • 1970-01-01
  • 2018-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多