android.content.res.Resources $ NotFoundException:使用RecyclerView时资源ID#0x0异常

时间:2016-09-23 14:20:32

标签: java android exception android-recyclerview adapter

我正在学习Android开发。在我的应用程序中,我创建了一个RecyclerView适配器:

 package com.asvadi.mabna;

    import android.graphics.Typeface;
    import android.graphics.drawable.Drawable;
    import android.support.v7.widget.CardView;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.view.View;
    import android.widget.TextView;

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

    private String[] captions;
    private int[] imageIds;

    private Listener listener;

    public static interface Listener {
        public void onClick(int position);
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public CaptionedImagesAdapter(String[] captions, int[] imageIds) {
        this.captions = captions;
        this.imageIds = imageIds;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private CardView cardView;
        public ViewHolder(CardView v) {
            super (v);
            cardView = v;
            Typeface iranSans = Typeface.createFromAsset(cardView.getContext().getAsset(), "fonts/iran_sans_ultra_light.ttf");
            TextView text = (TextView) cardView.findViewById(R.id.info_text);
            text.setTypeface(iranSans);
        }
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CardView cv = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.card_captioned_image, parent, false);
        return new ViewHolder(cv);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        CardView cardView = holder.cardView;
        ImageView imageView = (ImageView)cardView.findViewById(R.id.info_image);
        Drawable drawable = cardView.getResources().getDrawable(imageIds[position]);
        imageView.setImageDrawable(drawable);
        imageView.setContentDescription(captions[position]);
        TextView textView = (TextView)cardView.findViewById(R.id.info_text);
        textView.setText(captions[position]);

        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return captions.length;
    }
}

它在我的主要活动中工作正常,但是当我想在另一个活动中将此适配器附加到RecyclerView时,会出现问题。首先,我尝试存储此类的信息:

public class List {

        private String name;
        private int imageRe

sourceId;

    private List(String name, int imageResourceId) {
        this.name = name;
        this.imageResourceId= imageResourceId;
    }

    public static final List[] itemsOne = {
        new List("مقدمه",R.drawable.language),
        new List("تعریف برنامه نویسی",R.drawable.language),
        new List("تعریف زبان برنامه نویسی",R.drawable.language),
        new List("کد منبع",R.drawable.language),
        new List("زبان های کامپایل و تفسیری",R.drawable.language),
        new List("چرا جاوا اسکریپت",R.drawable.language),
        new List("اولین برنامه ما",R.drawable.language),
        new List("ددرخواست ورودی",R.drawable.language)
    };

在我的第二个活动中的2个数组中就像这些一样:

String[] itemsNames = new String[List.itemsOne.length];
                for (int i = 0; i > itemsNames.length; i++) {
                    itemsNames[i] = List.itemsOne[i].getName();
                }

                int[] itemsImages = new int[List.itemsOne.length];
                for (int i = 0; i > itemsImages.length; i++) {
                    itemsImages[i] = List.itemsOne[i].getImageResourceId();
                }

并将它们传递给我的适配器构造函数:

RecyclerView listRecycler = (RecyclerView) findViewById(R.id.list_recycler);

CaptionedImagesAdapter adapter = new CaptionedImagesAdapter(itemsNames,itemsImages);
listRecycler.setAdapter(adapter);

GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
listRecycler.setLayoutManager(layoutManager);

但我得到了这个例外:

09-23 17:32:17.551 14200-14200/com.asvadi.mabna E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.asvadi.mabna, PID: 14200
                                                                  android.content.res.Resources$NotFoundException: Resource ID #0x0
                                                                      at android.content.res.Resources.getValue(Resources.java:2495)
                                                                      at android.content.res.Resources.getDrawable(Resources.java:1987)
                                                                      at android.content.res.Resources.getDrawable(Resources.java:1973)
                                                                      at android.content.res.Resources.getDrawable(Resources.java:1947)
                                                                      at com.asvadi.mabna.CaptionedImagesAdapter.onBindViewHolder(CaptionedImagesAdapter.java:75)
                                                                      at com.asvadi.mabna.CaptionedImagesAdapter.onBindViewHolder(CaptionedImagesAdapter.java:16)
                                                                      at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5822)
                                                                      at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5855)
                                                                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5091)
                                                                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4967)
                                                                      at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
                                                                      at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:541)
                                                                      at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
                                                                      at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
                                                                      at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
                                                                      at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3315)
                                                                      at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3124)
                                                                      at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3568)
                                                                      at android.view.View.layout(View.java:16992)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5409)
                                                                      at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131)
                                                                      at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
                                                                      at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1364)
                                                                      at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:852)
                                                                      at android.view.View.layout(View.java:16992)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5409)
                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                      at android.view.View.layout(View.java:16992)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5409)
                                                                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)
                                                                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)
                                                                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1465)
                                                                      at android.view.View.layout(View.java:16992)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5409)
                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                      at android.view.View.layout(View.java:16992)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5409)
                                                                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)
                                                                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)
                                                                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1465)
                                                                      at android.view.View.layout(View.java:16992)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5409)
                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                      at android.view.View.layout(View.java:16992)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5409)
                                                                      at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2483)
                                                                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2185)
                                                                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1314)
                                                                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7057)
                                                                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:829)
                                                                      at android.view.Choreographer.doCallbacks(Choreographer.java:606)
                                                                      at android.view.Choreographer.doFrame(Choreographer.java:576)
                                                                    at a

这是我的项目依赖项:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.android.support:cardview-v7:24.2.0'
    compile 'com.android.support:recyclerview-v7:24.2.0'
    compile 'com.android.support:design:24.2.0'
}

和我的第二项活动:

package com.asvadi.mabna;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;

public class LessonDetail extends AppCompatActivity {

    public static final String EXTRA_LESSONNO = "lessnno";

    Toolbar tb;
    CollapsingToolbarLayout ctl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lesson_detail);

        //reference to toolbar and change enable home button
        tb = (Toolbar) findViewById(R.id.tb);
        setSupportActionBar(tb);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        //reference to ctl and settings its font
        Typeface iranSans = Typeface.createFromAsset(getAssets(), "fonts/iran_sans_ultra_light.ttf");

        //Get a reference to the intent and pulling out extra information to create the proper view
        Intent intent = getIntent();
        int position = intent.getIntExtra(EXTRA_LESSONNO, 0);

        //Reference to the views in the activity to be used in a case statement
        ImageView imageLesson = (ImageView) findViewById(R.id.image_lesson);
        ctl = (CollapsingToolbarLayout) findViewById(R.id.collapseit);

        ctl.setCollapsedTitleTypeface(iranSans);
        ctl.setExpandedTitleTypeface(iranSans);
        ctl.setCollapsedTitleTextColor(Color.WHITE);
        ctl.setExpandedTitleColor(Color.WHITE);

        switch (position) {
            case 0:
                imageLesson.setImageResource(Lesson.lessons[position].getImageResourceId());
                ctl.setTitle(Lesson.lessons[position].getName());

                RecyclerView listRecycler = (RecyclerView) findViewById(R.id.list_recycler);

                String[] itemsNames = new String[List.itemsOne.length];
                for (int i = 0; i > itemsNames.length; i++) {
                    itemsNames[i] = List.itemsOne[i].getName();
                }

                int[] itemsImages = new int[List.itemsOne.length];
                for (int i = 0; i > itemsImages.length; i++) {
                    itemsImages[i] = List.itemsOne[i].getImageResourceId();
                }

                CaptionedImagesAdapter adapter = new CaptionedImagesAdapter(itemsNames,itemsImages);
                listRecycler.setAdapter(adapter);

                GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
                listRecycler.setLayoutManager(layoutManager);
                break;
            case 1:
                imageLesson.setImageResource(Lesson.lessons[position].getImageResourceId());
                ctl.setTitle(Lesson.lessons[position].getName());
                break;
            case 2:
                imageLesson.setImageResource(Lesson.lessons[position].getImageResourceId());
                ctl.setTitle(Lesson.lessons[position].getName());
                break;
            case 3:
                imageLesson.setImageResource(Lesson.lessons[position].getImageResourceId());
                ctl.setTitle(Lesson.lessons[position].getName());
                break;
            case 4:
                imageLesson.setImageResource(Lesson.lessons[position].getImageResourceId());
                ctl.setTitle(Lesson.lessons[position].getName());
                break;
            case 5:
                imageLesson.setImageResource(Lesson.lessons[position].getImageResourceId());
                ctl.setTitle(Lesson.lessons[position].getName());
                break;
            case 6:
                imageLesson.setImageResource(Lesson.lessons[position].getImageResourceId());
                ctl.setTitle(Lesson.lessons[position].getName());
                break;
            case 7:
                imageLesson.setImageResource(Lesson.lessons[position].getImageResourceId());
                ctl.setTitle(Lesson.lessons[position].getName());
                break;
            case 8:
                imageLesson.setImageResource(Lesson.lessons[position].getImageResourceId());
                ctl.setTitle(Lesson.lessons[position].getName());
                break;
            case 9:
                imageLesson.setImageResource(Lesson.lessons[position].getImageResourceId());
                ctl.setTitle(Lesson.lessons[position].getName());
                break;
            case 10:
                imageLesson.setImageResource(Lesson.lessons[position].getImageResourceId());
                ctl.setTitle(Lesson.lessons[position].getName());
                break;
            case 11:
                imageLesson.setImageResource(Lesson.lessons[position].getImageResourceId());
                ctl.setTitle(Lesson.lessons[position].getName());
                break;
            case 12:
                imageLesson.setImageResource(Lesson.lessons[position].getImageResourceId());
                ctl.setTitle(Lesson.lessons[position].getName());
                break;
        }
    }
}

提前致谢...

2 个答案:

答案 0 :(得分:0)

试试这个: 在适配器的构造函数中发送Context:

Context context; 
public CaptionedImagesAdapter(String[] captions, int[] imageIds,Context context) {
    this.captions = captions;
    this.imageIds = imageIds;
    this.context=context;
}

然后,每次使用资源时都要使用此上下文:

 Drawable drawable = context.getResources().getDrawable(imageIds[position]);

答案 1 :(得分:0)

您可以使用以下内容,

Drawable drawable = context.getResources().getDrawable(imageIds[position]);

因为你需要drawable来识别图像的资源ID。