我有一个浪费时间的问题。直到昨天我一直在为我的项目做得很好。昨天早上我开始工作时,遇到了这个问题;
“二进制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();
答案 0 :(得分:0)
删除xml中的起始空间。(仅在<?xml version="1.0" encoding="utf-8"?>
之前)
原因:Xml声明应位于所有文档内容之前。