获取错误的图像索引

Kamalesh M. Talaviya

数组.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer-array name="animal">
        <item>@drawable/animal1</item>
        <item>@drawable/animal10</item>
        <item>@drawable/animal100</item>
        <item>@drawable/animal101</item>
        <item>@drawable/animal102</item>
        <item>@drawable/animal103</item>
        <item>@drawable/animal104</item>
        <item>@drawable/animal105</item>
        <item>@drawable/animal106</item>
        <item>@drawable/animal107</item>
        <item>@drawable/animal108</item>
        <item>@drawable/animal109</item>
        <item>@drawable/animal11</item>
        <item>@drawable/animal110</item>
    </integer-array>
</resources>

主Activity.class

在这里,我将动物数组的值放入 imgs (TypedArray)

TypedArray imgs = getResources().obtainTypedArray(R.array.animal);

现在检索资源 ID 并像这样存储到整数的 ArrayList 中

ArrayList<Integer> myImageList = new ArrayList<>();
for(int i=0; i<mystikers.length(); i++){
    myImageList.add(imgs.getResourceId(i, -1));
}

现在当设置图像时,它给出错误的图像,并且多次给出相同的图像

Glide.with(mContext).load(myImageList.get(5))
                    .override(150, 150)
                    .into(imageView);

如果我使用 recycle() 来回收图像 (TypedArray)

TypedArray imgs = getResources().obtainTypedArray(R.array.animal);
ArrayList<Integer> myImageList = new ArrayList<>();
    for(int i=0; i<imgs.length(); i++){
        myImageList.add(imgs.getResourceId(i, -1));
    }
imgs.recycle();

它显示这个错误

java.lang.RuntimeException: Cannot make calls to a recycled instance!

我的 gridview 适配器出现错误

public class StikAdpter extends BaseAdapter {
    private Context mContext;

    int size=0;
    ArrayList<Integer> img_List = new ArrayList<>();
    ArrayList<Integer> myImageList22 = new ArrayList<>();

    public StikAdpter(Context c,ArrayList<Integer> img_Detail_List,TypedArray mystikerishere){
        mContext= c;
        img_List=img_Detail_List;
        size = img_List.size();
        Log.v("count",String.valueOf(size));
        for(int i=0; i<mystikerishere.length(); i++){
            myImageList22.add(mystikerishere.getResourceId(i, -1));
            Log.v("allimages",img_List.get(i)+"");
        }
        mystikerishere.recycle();
    }
    @Override
    public int getCount() {
        return size;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View gridView1;

        if (convertView == null) {
            gridView1 = new View(mContext);

            // get layout from mobile.xml
            gridView1 = inflater.inflate(R.layout.grid_items, null);

            // set image based on selected text
            ImageView imageView = (ImageView) gridView1
                    .findViewById(R.id.grid_item_image);

            Glide.with(mContext)
                    .load(myImageList22.get(position))
                    .override(150, 150)
                    .into(imageView);
            gridView1.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, 200));
        }else {
            gridView1 = (View) convertView;
        }

        return gridView1;
    }
}
鲁朱尔·甘地

请尝试下面的代码,将其与您现有的代码进行比较,然后问我您是否会感到困惑。

public class StikAdpter extends BaseAdapter {
    private Context mContext;

    ArrayList<Integer> myImageList22 = new ArrayList<>();

    public StikAdpter(Context c, ArrayList<Integer> img_Detail_List, TypedArray mystikerishere) {
        mContext = c;
        for (int i = 0; i < mystikerishere.length(); i++) {
            myImageList22.add(mystikerishere.getResourceId(i, -1));
        }
        mystikerishere.recycle();
    }

    @Override
    public int getCount() {
        return myImageList22.size();
    }

    @Override
    public Object getItem(int position) {
        return myImageList22.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View gridView1;
        ImageView imageView;
        if (convertView == null) {
            gridView1 = new View(mContext);
            // get layout from mobile.xml
            gridView1 = inflater.inflate(R.layout.grid_items, null);
            imageView = (ImageView) gridView1
                    .findViewById(R.id.grid_item_image);

            // set image based on selected text
            gridView1.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, 200));
        } else {
            gridView1 = (View) convertView;
        }


        Glide.with(mContext)
                .load(myImageList22.get(position))
                .override(150, 150)
                .into(imageView);

        return gridView1;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

获取错误无效索引

来自分类Dev

错误:从URL获取图像

来自分类Dev

获取给定图像位置的numpy索引

来自分类Dev

onclick在for循环javascript中获取图像索引

来自分类Dev

图像加密获取填充错误

来自分类Dev

图像传递期间索引越界错误

来自分类Dev

选择排序获取错误的数组索引

来自分类Dev

python枚举函数获取索引错误

来自分类Dev

获取指定索引对象时出现错误

来自分类Dev

从 firebase 获取数据但面临索引错误

来自分类Dev

在 Python 中获取索引越界错误

来自分类Dev

TCPDF错误:无法获取图像的大小

来自分类Dev

cv :: cvtColor获取错误的尺寸图像

来自分类Dev

使用MongoDB获取错误的索引键模式错误

来自分类Dev

如何从位图数据(从数组)获取位图图像的索引?

来自分类Dev

jQuery / Dropzone.js获取添加图像的当前索引

来自分类Dev

获取为图像生成错误的图像-Google App脚本

来自分类Dev

从Viber的键盘按钮的图像中获取错误的图像

来自分类Dev

错误编号1919在为图像列创建索引期间

来自分类Dev

获取错误“ React Native上的图像源错误”

来自分类Dev

创建随机数组并获取索引中的值以及输入值但获取的信息错误时的索引

来自分类Dev

错误:如何通过元素的位置索引从列表中获取元素?

来自分类Dev

在MATLAB中获取索引超过矩阵尺寸错误

来自分类Dev

从原始错误获取排序数组的索引位置

来自分类Dev

-获取向量索引时出现Wsign转换错误

来自分类Dev

在SQL Server 2008中重建索引时获取错误

来自分类Dev

PHP PDO通过未定义的索引错误获取

来自分类Dev

尝试获取从CollectionBase继承的类的索引时收到以下错误:无法使用[]进行索引

来自分类Dev

jQuery:获取图像加载错误的详细信息

Related 相关文章

  1. 1

    获取错误无效索引

  2. 2

    错误:从URL获取图像

  3. 3

    获取给定图像位置的numpy索引

  4. 4

    onclick在for循环javascript中获取图像索引

  5. 5

    图像加密获取填充错误

  6. 6

    图像传递期间索引越界错误

  7. 7

    选择排序获取错误的数组索引

  8. 8

    python枚举函数获取索引错误

  9. 9

    获取指定索引对象时出现错误

  10. 10

    从 firebase 获取数据但面临索引错误

  11. 11

    在 Python 中获取索引越界错误

  12. 12

    TCPDF错误:无法获取图像的大小

  13. 13

    cv :: cvtColor获取错误的尺寸图像

  14. 14

    使用MongoDB获取错误的索引键模式错误

  15. 15

    如何从位图数据(从数组)获取位图图像的索引?

  16. 16

    jQuery / Dropzone.js获取添加图像的当前索引

  17. 17

    获取为图像生成错误的图像-Google App脚本

  18. 18

    从Viber的键盘按钮的图像中获取错误的图像

  19. 19

    错误编号1919在为图像列创建索引期间

  20. 20

    获取错误“ React Native上的图像源错误”

  21. 21

    创建随机数组并获取索引中的值以及输入值但获取的信息错误时的索引

  22. 22

    错误:如何通过元素的位置索引从列表中获取元素?

  23. 23

    在MATLAB中获取索引超过矩阵尺寸错误

  24. 24

    从原始错误获取排序数组的索引位置

  25. 25

    -获取向量索引时出现Wsign转换错误

  26. 26

    在SQL Server 2008中重建索引时获取错误

  27. 27

    PHP PDO通过未定义的索引错误获取

  28. 28

    尝试获取从CollectionBase继承的类的索引时收到以下错误:无法使用[]进行索引

  29. 29

    jQuery:获取图像加载错误的详细信息

热门标签

归档