【问题标题】:Error Resource ID #0x0 using setImageResource Android Adapter RecyclerView错误资源 ID #0x0 使用 setImageResource Android Adapter RecyclerView
【发布时间】:2016-04-07 19:30:50
【问题描述】:

我正在学习 Android,但我的适配器有问题。我可以在适配器中获取文本,但是设置图像源时遇到问题。

[This is the result if i comment the code ]

[this is an example of what i want but setting the images from my array.

我在arrays.xml中使用带有可绘制项目的整数数组

<integer-array name="champ_image">
        <item>@drawable/aatrox</item>
        <item>@drawable/ahri</item>
        <item>@drawable/akali</item>
        <item>@drawable/alistar</item>
        <item>@drawable/amumu</item>
        <item>@drawable/anivia</item>
        <item>@drawable/ashe</item>
    </integer-array>

这是我的错误日志:

01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime: FATAL EXCEPTION: main
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime: Process: com.sagar.materialdesigndemo, PID: 19392
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime: android.content.res.Resources$NotFoundException: Resource ID #0x0
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime:     at android.content.res.Resources.getValue(Resources.java:1307)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime:     at android.content.res.Resources.getDrawable(Resources.java:826)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime:     at android.content.Context.getDrawable(Context.java:403)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime:     at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:26)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime:     at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:321)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime:     at android.support.v7.internal.widget.TintManager.getDrawable(TintManager.java:177)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime:     at android.support.v7.internal.widget.TintManager.getDrawable(TintManager.java:170)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime:     at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:53)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime:     at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:74)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime:     at com.sagar.adapters.ChampAdapter.onBindViewHolder(ChampAdapter.java:55)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime:     at com.sagar.adapters.ChampAdapter.onBindViewHolder(ChampAdapter.java:15)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime:     at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5212)

这是我的适配器,名为ChampAdapter,我认为问题出在:holder.champImg.setImageResource(champItem.get(position).champImg); //HERE

public class ChampAdapter extends RecyclerView.Adapter<ChampAdapter.ViewHolder> {

    public List<ChampionItemModel> champItem;

    public ChampAdapter(List<ChampionItemModel> champItem){this.champItem = champItem;}

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView champName;
        TextView roleChamp;
        ImageView champImg;

        public ViewHolder(View itemView) {
            super(itemView);
            this.champName = (TextView)itemView.findViewById(R.id.champ_name);
            this.roleChamp = (TextView)itemView.findViewById(R.id.champ_role);
            this.champImg = (ImageView)itemView.findViewById(R.id.champ_image);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_champs,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.itemView.setClickable(true);
        holder.itemView.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v){

            }
        });

        holder.champName.setText(champItem.get(position).champName);
        holder.roleChamp.setText(champItem.get(position).roleChamp);
        holder.champImg.setImageResource(champItem.get(position).champImg); //HERE IS THE PROBLEM
    }

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

}

这是剩下的代码

  • 型号:

    public class ChampionItemModel {
    
        public String champName;
        public String roleChamp;
        public int champImg;
    
        public ChampionItemModel(String champName, String roleChamp, int champImg){
            this.champName = champName;
            this.roleChamp = roleChamp;
            this.champImg = champImg;
        }
    }
    
  • 片段方法调用initializeChampsItemList:

    private void initializeChampsItemList(){
            ChampionItemModel championItemModel;
            String[] champsNames = getResources().getStringArray(R.array.champ_name);
            String[] champsRoles = getResources().getStringArray(R.array.champ_role);
            int[] champsImages = getResources().getIntArray(R.array.champ_image);
            final int length = champsNames.length;
            for(int i=0;i<length;i++){
                championItemModel = new ChampionItemModel(champsNames[i], champsRoles[i], champsImages[i]);
                champItems.add(championItemModel);
            }
    
        }
    

【问题讨论】:

  • 显示champ_image 数组xml
  • 我已经用 arrays.xml 更新了帖子
  • 查看我的回答,帮助您解决问题。谢谢

标签: android android-studio android-recyclerview material-design


【解决方案1】:

我假设您正在尝试从可绘制文件夹加载图像。 然后声明 'champsImages' 数组,如下所示:

int[] champsImages = {R.drawable.image1, R.drawable.image2, R.drawable.image3, ... to more};

我认为这将有助于实现您的预​​期结果。

【讨论】:

    【解决方案2】:

    arrays.xml champ_image 数组项是@drawable/aatrox 意味着champ_image 是输入数组而不是int。所以使用obtainTypedArray 方法返回TypedArray 为:

    TypedArray imgsTypedArray = getResources().obtainTypedArray(R.array.champ_image);
    

    现在使用 TypedArray.getResourceId 获取可绘制的 id 并将其传递给 ChampionItemModel 作为:

    for(int i=0;i<length;i++){
      int drawableId= imgsTypedArray.getResourceId(i, -1);
      championItemModel = new ChampionItemModel(champsNames[i], 
                                     champsRoles[i], 
                                     drawableId);
      champItems.add(championItemModel);
    }
    

    【讨论】:

    • 天啊,你是最棒的,非常感谢。 =D!!
    【解决方案3】:

    http://developer.android.com/guide/topics/resources/more-resources.html#TypedArray

    保存在 res/values/arrays.xml 的 XML 文件:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
       <array name="icons">
         <item>@drawable/home</item>
         <item>@drawable/settings</item>
         <item>@drawable/logout</item>
       </array>
       <array name="colors">
         <item>#FFFF0000</item>
         <item>#FF00FF00</item>
         <item>#FF0000FF</item>
       </array>
    </resources>
    

    此应用程序代码检索每个数组,然后获取每个数组中的第一个条目:

    Resources res = getResources();
    TypedArray icons = res.obtainTypedArray(R.array.icons);
    Drawable drawable = icons.getDrawable(0);
    
    TypedArray colors = res.obtainTypedArray(R.array.colors);
    int color = colors.getColor(0,0);
    

    【讨论】:

      猜你喜欢
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多