android.view.InflateException:二进制 XML 文件第 22 行

android.view.InflateException: Binary XML file line #22

我刚开始学习 Android 并且在本教程中遇到了障碍,花了很多时间来解决这个问题但无法解决。我该如何解决这个问题?

我正在学习本教程: http://frogermcs.github.io/Instagram-with-Material-Design-concept-is-getting-real/

我想在 RecycleView 中显示项目,但没有显示任何内容,每当我在 itemCount 中添加整数时,我都会收到如下所述的错误。

这是错误:

android.view.InflateException: Binary XML file line #22: Error inflating class learningAndroid.view.SquaredFrameLayout

并指向这一行。

at learningAndroid.Adapter.FeedAdapter.onCreateViewHolder(FeedAdapter.java:34)

<?xml version="1.0" encoding="utf-8"?><!-- item_feed.xml -->
<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:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="8dp"
card_view:cardCornerRadius="4dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!-- Add user profile image -->
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/wink" />

    <learningAndroid.view.SquaredImageView
        android:id="@+id/vImageRoot"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/ivFeedCenter"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />
    </learningAndroid.view.SquaredImageView>

    <ImageView
        android:id="@+id/ivFeedBottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

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

这是我的代码。

package learningAndroid.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import learningAndroid.R;
import learningAndroid.ui.Utils;
import learningAndroid.view.SquaredImageView;

/**
 * Created by Win 7 on 7/8/2015.
 */

public class FeedAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int ANIMATED_ITEMS_COUNT = 2;

    private Context context;
    private int lastAnimatedPosition = -1;
    private int itemsCount;

    public FeedAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_feed, parent, false);
        return new CellFeedViewHolder(view);
    }

    private void runEnterAnimation(View view, int position) {
        if (position >= ANIMATED_ITEMS_COUNT - 1) {
            return;
        }

        if (position > lastAnimatedPosition) {
            lastAnimatedPosition = position;
            view.setTranslationY(Utils.getScreenHeight(context));
            view.animate()
                    .translationY(0)
                    .setInterpolator(new DecelerateInterpolator(3.f))
                    .setDuration(700)
                    .start();
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        runEnterAnimation(viewHolder.itemView, position);
        CellFeedViewHolder holder = (CellFeedViewHolder) viewHolder;
        if (position % 2 == 0) {
            holder.ivFeedCenter.setImageResource(R.drawable.wink);
            holder.ivFeedBottom.setImageResource(R.drawable.wink);
        } else {
            holder.ivFeedCenter.setImageResource(R.drawable.wink);
            holder.ivFeedBottom.setImageResource(R.drawable.wink);
        }
    }

    @Override
    public int getItemCount() {
        return itemsCount;
    }

    public static class CellFeedViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.ivFeedCenter)
        SquaredImageView ivFeedCenter;
        @InjectView(R.id.ivFeedBottom)
        ImageView ivFeedBottom;

        public CellFeedViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    public void updateItems() {
        itemsCount = 10;
        notifyDataSetChanged();
    }
}

我认为你的问题在于:

<learningAndroid.view.SquaredImageView
    android:id="@+id/vImageRoot"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<ImageView
    android:id="@+id/ivFeedCenter"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop" />
</learningAndroid.view.SquaredImageView>

如果您使用与 this and this 类似的代码,您的 ImageView 不能在 SquaredImageView 内,因为 SquaredImageView 不是布局,它从 `ImageView.

你应该把它改成SquaredFrameLayout