当前位置: 首页 > 知识库问答 >
问题:

在RecolyerView上inflating类ImageView时出错

殷烨
2023-03-14

我用最小SDK 15和目标23构建了一个应用程序,我在recyclerview中有不同的视图。Apps在android版本=>5.0上运行良好,但在android版本<5.0上崩溃,出现以下错误消息

下面是我的RecyclerAdapter的代码:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> { // line 27

    private static final int Image_digit = 1;
    private static final int Map_digit = 2;
    private static final int Text_digit = 3;
    private Context context;




    private List<FeedAdapter> mDataList;
    private LayoutInflater inflater;

    public RecyclerAdapter(Context context, List<FeedAdapter> data) {
        inflater = LayoutInflater.from(context);
        this.mDataList = data;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {  // Create the Prime and Non-Prime row Layouts

            case Image_digit:

                ViewGroup imageView = (ViewGroup) inflater.inflate(R.layout.list_item, parent, false);
                MyViewHolder_Images holderPrime = new MyViewHolder_Images(imageView);
                return holderPrime;

            case Map_digit:

                ViewGroup mapView = (ViewGroup) inflater.inflate(R.layout.list_item_map, parent, false);
                MyViewHolder_Map holderNonPrime = new MyViewHolder_Map(mapView);
                return holderNonPrime;

            default:

                ViewGroup defaultRow = (ViewGroup) inflater.inflate(R.layout.list_item_text, parent, false); // line 64
                MyViewHolder_Text holderDefault = new MyViewHolder_Text(defaultRow);
                return holderDefault;
        }
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        FeedAdapter current = mDataList.get(position);

        switch (holder.getItemViewType()) {

            case Image_digit:

                MyViewHolder_Images holder_prime = (MyViewHolder_Images) holder;
                holder_prime.setData(current);

                break;

            case Map_digit:

                MyViewHolder_Map holder_not_prime = (MyViewHolder_Map) holder;
                holder_not_prime.setData(current);

                break;
            default:
                MyViewHolder_Text holder_not_text = (MyViewHolder_Text) holder;
                holder_not_text.setData(current);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    @Override // This will help us to determine ROW TYPE : i.e. the PRIME or NON-PRIME row.
    public int getItemViewType(int position) {

        FeedAdapter feedAdapter = mDataList.get(position);
        if (feedAdapter.getViewInt() == 1)
            return Image_digit;
        else if (feedAdapter.getViewInt() == 2){
            return Map_digit;
        }
        else if (feedAdapter.getViewInt() == 3){
            return Text_digit;
        }
        else {
            return 0;
        }

    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        public MyViewHolder(View itemView) {
            super(itemView);
        }
    }

    // Holder class for Images rows
    public class MyViewHolder_Images extends MyViewHolder implements View.OnClickListener {

        TextView title;
        ImageView imgThumb, imgRowType, menu;

        public MyViewHolder_Images(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.tvTitle);
            imgThumb = (ImageView) itemView.findViewById(R.id.img_row);
            imgRowType = (ImageView) itemView.findViewById(R.id.img_row2);
            menu   = (ImageView) itemView.findViewById(R.id.img_row_delete);
        }

        public void setData(final FeedAdapter current) {
            this.title.setText(current.getTitle());
            this.imgThumb.setImageResource(current.getImageID());
            this.imgRowType.setImageResource(R.drawable.thumb_1_4);
            this.menu.setOnClickListener(MyViewHolder_Images.this);
            this.imgThumb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showImage(current.getImageID());
                }
            });


        }
        @Override
        public void onClick(View v) {
            Log.i("onClick"," i was clicked");
            openDialog();

        }
    }

    // Holder class for maps rows
    public class MyViewHolder_Map extends MyViewHolder implements View.OnClickListener {

        TextView title;
        ImageView imgThumb, imgRowType, menu;

        public MyViewHolder_Map(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.tvTitle);
            imgThumb = (ImageView) itemView.findViewById(R.id.img_row);
            imgRowType = (ImageView) itemView.findViewById(R.id.img_row2);
            menu   = (ImageView) itemView.findViewById(R.id.img_row_delete);
        }

        public void setData(final FeedAdapter current) {
            this.title.setText(current.getTitle());
            this.imgThumb.setImageResource(current.getImageID());
            this.imgRowType.setImageResource(R.drawable.thumb_1_4);
            this.menu.setOnClickListener(MyViewHolder_Map.this);
            this.imgThumb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showImage(current.getImageID());
                }
            });


        }
        @Override
        public void onClick(View v) {
            Log.i("onClick","i was clicked");
            openDialog();





        }
    }
    // Holder class for NON-Text rows
    public class MyViewHolder_Text extends MyViewHolder implements View.OnClickListener {

        TextView title;
        ImageView imgThumb, menu;

        public MyViewHolder_Text(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.tvTitle);
            imgThumb = (ImageView) itemView.findViewById(R.id.img_row);
            menu   = (ImageView) itemView.findViewById(R.id.img_row_delete);

        }

        public void setData(final FeedAdapter current) {
            this.title.setText(current.getTitle());
            this.imgThumb.setImageResource(current.getImageID());
            this.menu.setOnClickListener(MyViewHolder_Text.this);
            this.imgThumb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showImage(current.getImageID());
                }
            });

        }
        @Override
        public void onClick(View v) {
            Log.i("onClick","i was clicked");
            openDialog();



        }}


    public void openDialog() {
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        LinearLayout.LayoutParams dialogParams = new LinearLayout.LayoutParams(
                400, 256);//set height and width here, ie (width,height)

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View dislogView = inflater
                .inflate(R.layout.feed_menu, null);
        dialog.setContentView(dislogView, dialogParams);
        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.text);


        TextView dialogButton = (TextView) dialog.findViewById(R.id.report);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();
    }

    public void showImage(int imageUri) {
        Dialog builder = new Dialog(context);
        builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
        builder.getWindow().setBackgroundDrawable(
                new ColorDrawable(android.graphics.Color.TRANSPARENT));
        builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                //nothing;
            }
        });

        ImageView imageView = new ImageView(context);
        imageView.setImageResource(imageUri);
        builder.addContentView(imageView, new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        builder.show();
    }
}

list_item_text.xml

<?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="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    card_view:cardCornerRadius="5dp">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/img_row"
                android:layout_width="55dp"
                android:layout_height="57.3dp"
                android:scaleType="fitXY"
                android:layout_margin="7dp"
                android:src="@drawable/thumb_1_0"/>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_marginTop="4dp">
                    <TextView
                        android:id="@+id/tvTitle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/title"
                        android:textColor="#000000"
                        android:textSize="12sp"
                        android:textStyle="bold"/>
                    <TextView
                        android:id="@+id/tv_user_tag"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/tag"
                        android:textColor="#616161"
                        android:textSize="9sp"
                        android:layout_marginLeft="5dp" />

                </LinearLayout>

                <TextView
                    android:id="@+id/tvDescription"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/description"
                    android:layout_marginTop="2dp"
                    android:layout_marginBottom="2dp"
                    android:textSize="9sp"
                    android:textStyle="italic"/>
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <View
                        android:layout_width="1dp"
                        android:layout_height="fill_parent"
                        android:background="#FF0000FF" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:text="@string/lorem"
                        android:maxLines="2"
                        android:ellipsize="end"/>

                </LinearLayout>

            </LinearLayout>

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

                <ImageView
                    android:id="@+id/img_row_delete"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_margin="7dp"
                    android:contentDescription="@string/delete_image"
                    android:src="@drawable/more"
                    android:tint="@color/colorPrimary"
                    android:clickable="true" />

            </LinearLayout>

        </LinearLayout>

        <View
            android:layout_width="fill_parent"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="6.3dp"
            android:layout_marginRight="6.3dp"
            android:layout_height="1dp"
            android:background="@color/colorPrimary" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/time_of_post"
                android:text="about 5 min. ago"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="7dp"/>
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="45dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="3dp">

                <ImageView
                    android:layout_width="20dp"
                    android:id="@+id/comment_count_img"
                    android:src="@drawable/comment_count"
                    android:layout_height="20dp"
                    android:layout_marginRight="3dp"
                    android:layout_toLeftOf="@+id/comment_count_no"
                    android:clickable="true" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/comment_count_no"
                    android:text="10"
                    android:layout_toLeftOf="@+id/comment_like_img"
                    android:layout_marginRight="5dp"/>

                <ImageView
                    android:layout_width="20dp"
                    android:id="@+id/comment_like_img"
                    android:src="@drawable/like_count"
                    android:layout_marginLeft="6.7dp"
                    android:layout_height="20dp"
                    android:layout_toLeftOf="@+id/comment_like_no"
                    android:layout_marginRight="3dp"
                    android:clickable="true" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/comment_like_no"
                    android:text="10"
                    android:layout_toLeftOf="@+id/comment_dislike_img"
                    android:layout_marginRight="5dp"/>

                <ImageView
                    android:layout_width="20dp"
                    android:id="@+id/comment_dislike_img"
                    android:src="@drawable/thumbs_down"
                    android:layout_height="20dp"
                    android:layout_marginLeft="6.7dp"
                    android:layout_toLeftOf="@+id/comment_dislike_no"
                    android:layout_marginRight="3dp"
                    android:clickable="true" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/comment_dislike_no"
                    android:text="10"
                    android:layout_alignParentRight="true"/>

            </RelativeLayout>

        </LinearLayout>

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

Build.Gradle

apply plugin: 'com.android.application'
android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"

    defaultConfig {
        applicationId "com.shixxels.thankgodrichard.spotpopfinal"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dexOptions {
        javaMaxHeapSize "4g"
    }
}

共有1个答案

李成礼
2023-03-14

如果你在你的ImageView中使用vectors图标作为背景,并且android的版本在5.0以下,ImageView就会导致这次崩溃。尝试使用 而不是 imageview,并且不要忘记在java代码中使用appCompatImageView声明和强制转换imageview。要将向量设置为背景,请使用以下xml属性: app:srccompat来选择颜色,请使用 android:tint,最后,在您的gradle文件中,您必须添加对向量的支持:

android {
    //...
    defaultConfig {
 //...

        vectorDrawables {
            useSupportLibrary = true
        }
    }

 类似资料: