作为RecycleViewAdapter的一部分,我在扩展XML资源时遇到了以下异常:
例外:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xcv.poke, PID: 16410
android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class <unknown>
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
我的PokeImageAdapter适配器类:
package com.xcv.poke.adapters;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import com.xcv.poke.R;
import java.util.ArrayList;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
public class PokeImageAdapter extends RecyclerView.Adapter<PokeImageAdapter.ViewHolder> {
//this context we will use to inflate the layout
private Context mCtx;
//we are storing all the products in a list
private ArrayList<String> urlList;
//getting the context and product list with constructor
public PokeImageAdapter(Context mCtx, ArrayList<String> fileUrlLinkList) {
this.mCtx = mCtx;
this.urlList = fileUrlLinkList;
}
public class ViewHolder extends RecyclerView.ViewHolder {
CardView imageViewHolder;
ImageView imageView;
ProgressBar progressBar;
public ViewHolder(View itemView) {
super(itemView);
imageViewHolder = itemView.findViewById(R.id.cv_imageHolder);
imageView = itemView.findViewById(R.id.img_thumbnail);
progressBar = itemView.findViewById(R.id.pb_loader);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//inflating and returning our view holder
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.cardview_pokemon_image_tile, null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final ViewHolder mHolder = holder;
final ProgressBar progressBar = holder.progressBar;
final int currentPosition = position;
//getting the product of the specified position
final String currentItemURL = urlList.get(position);
progressBar.setVisibility(View.VISIBLE);
Picasso.with(mCtx)
.load(currentItemURL)
.into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
mHolder.imageView.setImageBitmap(bitmap);
progressBar.setVisibility(View.GONE);
//stop progressbar here
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
progressBar.setVisibility(View.GONE);
//stop progressbar here
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
// Picasso.with(mCtx)
// .load(new File(currentItem.getLinkURL()))
// .into(holder.imageView);
// Picasso.with(getApplicationContext()).load(resultBmpURl).resize(400, 400).into(bmImage);
}
@Override
public int getItemCount() {
return urlList.size();
}
}
我的XML资源: cardview_pokemon_image_tile.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.CardView
android:id="@+id/cv_imageHolder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="5dp"
card_view:cardCornerRadius="10dp">
<RelativeLayout
android:id="@+id/img_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/pb_loader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:gravity="center"
android:indeterminateTint="@color/colorGreen"
android:padding="10dp" />
<ImageView
android:id="@+id/img_thumbnail"
android:layout_width="match_parent"
android:layout_height="260dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:padding="4dp"
android:scaleType="fitXY" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>