我刚刚开始学习Android并且在本教程中遇到了障碍,花了相当多的时间来解决这个问题,但无法实现。我该如何解决这个问题?
我正在关注本教程: http://frogermcs.github.io/Instagram-with-Material-Design-concept-is-getting-real/
我想在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();
}
}
这是我的代码。
GridViewRow row = GridView1.Rows[previouslySavedIndex];
答案 0 :(得分:1)
我认为你的问题在于:
<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和this类似的代码,则ImageView
不能位于SquaredImageView
内,因为SquaredImageView
不是布局,它会扩展来自`ImageView。
您应该将其更改为SquaredFrameLayout
。