android.view.InflateException:二进制XML文件行#0:二进制XML文件行#0:错误膨胀类android.support.v7.widget.CardView

时间:2018-01-13 06:16:25

标签: java android android-studio

您好我正在创建一个联系人应用程序,并在转换为新片段时遇到问题。我已经在这个问题上花了10个小时,它正把我赶到了墙上。

一旦我开始像我在学习一样,我遇到了这个问题,并完全失去了我应该做什么。 如果有人能提供指针,我将非常感激。

以下是代码的java类:

public class ContactPropertyListAdapter extends ArrayAdapter<String> {


    private LayoutInflater mInflater;
    private List<String> mProperties = null;
    private int layoutResource;
    private Context mContext;
    private String mAppend;


    public ContactPropertyListAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<String> properties) {
        super(context, resource, properties);
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutResource = resource;
        this.mContext = context;
        this.mProperties = properties;

    }

    //------------------ Stuff to change ---------------------------------------------------

    // in replacement of recycler view
    //change if more widgets wants to be added
    private static class ViewHolder {
        TextView property;
        ImageView rightIcon;
        ImageView leftIcon;

    }
    //--------------------------------------------------------------------------------------

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        /**
         ******* ViewHolder Build Pattern Start *******
         */

        // view holds widget in memory ahead of time for efficiency
        final ViewHolder holder;


        !! WHERE ERROR BEGINS !!
        if(convertView == null){
            convertView = mInflater.inflate(layoutResource, parent, false);
            holder = new ViewHolder();

            //------------------ Stuff to change ---------------------------------------------------
            holder.property = (TextView) convertView.findViewById(R.id.tvMiddleCardView);
            holder.rightIcon = (ImageView) convertView.findViewById(R.id.iconRightCardView);
            holder.leftIcon = (ImageView) convertView.findViewById(R.id.iconLeftCardView);
            //--------------------------------------------------------------------------------------

            //used to store view
            convertView.setTag(holder);
        }
        else{
            holder = (ViewHolder) convertView.getTag();
        }

        //------------------ Stuff to change ---------------------------------------------------

        final String property = getItem(position);
        holder.property.setText(property);


        //check if it's an email or a phone number
        //email
        if(property.contains("@")) {
            holder.leftIcon.setImageResource(mContext.getResources().getIdentifier("@drawable/ic_email", null, mContext.getPackageName()));
        }
        else if((property.length() != 0)){
            holder.leftIcon.setImageResource(mContext.getResources().getIdentifier("@drawable/ic_phone", null, mContext.getPackageName()));
            holder.rightIcon.setImageResource(mContext.getResources().getIdentifier("@drawable/ic_message", null, mContext.getPackageName()));
        }

        //--------------------------------------------------------------------------------------

        return convertView;
    }
}

logcat消息:

FATAL EXCEPTION: main
Process: com.example.joon.contactslist, PID: 3250


android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.v7.widget.CardView
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.example.joon.contactslist.Utils.ContactPropertyListAdapter.getView(ContactPropertyListAdapter.java:74)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.makeAndAddView(ListView.java:1875)
at android.widget.ListView.fillDown(ListView.java:702)
at android.widget.ListView.fillFromTop(ListView.java:763)
at android.widget.ListView.layoutChildren(ListView.java:1684)
at android.widget.AbsListView.onLayout(AbsListView.java:2148)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1166)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:851)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:870)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1166)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:851)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:870)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:67
01-12 22:05:50.442 1653-1664/system_process W/ActivityManager:   Force finishing activity com.example.joon.contactslist/.MainActivity
01-12 22:05:50.448 1228-1339/? D/PermissionCache: checking android.permission.READ_FRAME_BUFFER for uid=1000 => granted (312 us)
01-12 22:05:50.456 1228-1339/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 1327104

以及相关的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:id="@+id/cardView"
        app:cardElevation="3dp"
        android:layout_margin="10dp"
        android:layout_marginTop="30dp"
        android:background="@drawable/grey_border">

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

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_marginStart="20dp"
                android:id="@+id/iconLeftCardView"
                android:layout_centerVertical="true"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15sp"
                android:textColor="@color/black"
                android:layout_centerInParent="true"
                android:id="@+id/tvMiddleCardView"/>

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_marginEnd="20dp"
                android:layout_alignParentRight="true"
                android:id="@+id/iconRightCardView"
                android:layout_centerVertical="true"/>

        </RelativeLayout>

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

</RelativeLayout>

我的成绩如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.joon.contactslist"
        minSdkVersion 20
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

    compile "com.android.support:support-core-utils:26.1.0"

    //Android Support Design Library
    compile 'com.android.support:design:26.1.0'

    //Universal Image Loader
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

    //Circle ImageView library
    compile 'de.hdodenhof:circleimageview:2.2.0'

    //Cardview library
    compile 'com.android.support:cardview-v7:26.1.0'





}

1 个答案:

答案 0 :(得分:0)

我认为android:background="@drawable/grey_border"会导致问题。请将其删除并尝试

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:id="@+id/cardView"
        app:cardElevation="3dp"
        android:layout_margin="10dp"
        android:layout_marginTop="30dp">