【问题标题】:How to fix Error inflating class Android.support.v7.widget.CardView如何修复错误膨胀类 Android.support.v7.widget.CardView
【发布时间】:2015-10-15 00:44:13
【问题描述】:

我知道这个问题已经被问过很多次了,但没有一个能解决我的问题。我正在尝试在 RecyclerView 中使用 CardView 但到目前为止还没有运气。 这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<Android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:card_view="http://schemas.Android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_gravity="center"
  card_view:cardCornerRadius="4dp"
  card_view:cardBackgroundColor="#AA66CC"
  card_view:cardElevation="10dp"
  android:id="@+id/cv">

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_gravity="center">

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:id="@+id/list_item"
    android:text="asdasd asdasd tt"
    />
</RelativeLayout>

</Android.support.v7.widget.CardView>

这是我的适配器:

public class ApprovalListAdapter  extends Adapter<ApprovalListAdapter.ViewHolder> {

private String[] dataSource;
private Context mContext;
public ApprovalListAdapter(String[] dataArgs, Context context){
    dataSource = dataArgs;
    mContext = context;

}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // create a new view
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.approval_list_row, parent, false);

    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;


}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.textView.setText(dataSource[position]);
}


@Override
public int getItemCount() {
    return dataSource.length;
}

public static class ViewHolder extends RecyclerView.ViewHolder{
    protected TextView textView;
    protected CardView cv;
    public ViewHolder(View itemView) {
        super(itemView);
        cv = (CardView) itemView.findViewById(R.id.cv);
        textView =  (TextView) itemView.findViewById(R.id.list_item);

    }
  }
}

我还添加了所需的依赖项

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  compile 'com.android.support:appcompat-v7:23.0.1'
  compile 'com.android.support:support-v4:23.0.1'
  compile 'com.android.support:recyclerview-v7:23.0.1'
  compile 'com.android.support:cardview-v7:23.0.1'
  compile 'com.android.support:design:23.0.1'
  compile project(":volley")
}

即 CardView 不在 RecyclerView 中时可以正常工作。

【问题讨论】:

  • 不工作意味着您遇到的确切问题是什么?
  • android.view.InflateException: Binary XML file line #2: Error inflating class Android.support.v7.widget.CardView@pavan
  • 是什么都不显示还是至少显示一项
  • 它粉碎了应用程序@pavan

标签: java android android-recyclerview android-cardview


【解决方案1】:

您似乎有大小写错误,cardview 标签以小写开头。 在您的 xml 中,替换:

<Android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" ... </Android.support.v7.widget.CardView>

与:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" ... </android.support.v7.widget.CardView>

【讨论】:

  • 谢谢,解决了我的问题!
  • 谢谢!我输入了cardview 而不是CardView 也遇到了问题..
猜你喜欢
  • 1970-01-01
  • 2015-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-05
  • 1970-01-01
相关资源
最近更新 更多