使用回收视图时获取错误资源$ NotFoundException

时间:2018-05-06 05:44:36

标签: java android android-recyclerview

我是Android Develop的新手,我收到了这个错误:

  

android.content.res.Resources $ NotFoundException:资源ID#0x7f070075

每当我将适配器设置为回收视图myrv.setAdapter(myAdapter);时,一切似乎都被正确编码,我已经检查了很多次代码,我无法找到导致此问题的原因。

这是我的代码

package com.example.root.agenda;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    List<Contact> lstContact;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        lstContact = new ArrayList<>();


  lstContact.add(new Contact("Andrea", "5874-2563", "Normal", R.drawable.user_painted_black64));
    lstContact.add(new Contact("Daniela", "2013-2365", "Normal", R.drawable.user_black));
    lstContact.add(new Contact("Juan", "7584-2323", "Normal", R.drawable.user_painted64));
    lstContact.add(new Contact("Elena", "7541-2110", "Normal", R.drawable.user_painted_black64));
        lstContact.add(new Contact("Maria", "7504-2000", "Normal", R.drawable.user_painted_white64_1));
        lstContact.add(new Contact("Edgardo", "2048-2048", "Normal", R.drawable.user_painted_white64_1));
        lstContact.add(new Contact("Alfonso", "9514-3214", "Normal", R.drawable.user_painted_black64));
        lstContact.add(new Contact("Erick", "5412-3652", "Normal", R.drawable.user_painted_white64_1));
        lstContact.add(new Contact("Andrea", "5874-2563", "Normal", R.drawable.user_painted_black64));
        lstContact.add(new Contact("Daniela", "2013-2365", "Normal", R.drawable.user_black));

        RecyclerView myrv = (RecyclerView) findViewById(R.id.id_recyclerView);
        RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this, lstContact);
        myrv.setLayoutManager(new GridLayoutManager(this, 3));
        myrv.setAdapter(myAdapter);

     /*  FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });*/
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

对于Recycler View

    package com.example.root.agenda;
    import android.content.Context;
    import android.content.Intent;
    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.TextView;
    import java.util.List;
    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

    private Context mContext;
    private List<Contact> mData;

    public RecyclerViewAdapter(Context mContext, List<Contact> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view;
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        view = mInflater.inflate(R.layout.cardview_contact_layout, parent, false);
        return new MyViewHolder(view);

    }

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

        holder.tv_contact_name.setText(mData.get(position).getNombre());
        holder.img_contact_thumbnail.setImageResource(mData.get(position).getThumbnail());
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //pasando datos al detalle de contacto
                Intent intent = new Intent(mContext, Contact_details_activity.class);
                intent.putExtra("Nombre", mData.get(position).getNombre());
                intent.putExtra("Numero de Telefono", mData.get(position).getTelefono());
                intent.putExtra("Thumbnail", mData.get(position).getThumbnail());
                mContext.startActivity(intent);

            }
        });

        //Click Listener
    }

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

    public static class MyViewHolder extends RecyclerView.ViewHolder{

        TextView tv_contact_name;
        ImageView img_contact_thumbnail;
        CardView cardView;

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

            tv_contact_name = (TextView) itemView.findViewById(R.id.id_contact_name);
            img_contact_thumbnail = (ImageView) itemView.findViewById(R.id.id_contact_img);
            cardView = (CardView) itemView.findViewById(R.id.id_cardview);
        }
    }
}

3 个答案:

答案 0 :(得分:0)

如果不起作用,请尝试清理项目,然后尝试只为项目中确定存在的所有列表项目放置一个缩略图资源。如果这也有效,那么我们将尝试以不同的方式找到问题。

答案 1 :(得分:0)

R.java文件中搜索此地址(0x7f070075)。

您将找到导致崩溃的ID。这将帮助您修复崩溃,并避免将来的麻烦。

答案 2 :(得分:-1)

似乎代码中没有错误,最好发布logcat异常以了解发现问题

我使用了相同的代码并且工作正常,请找下面的代码进行比较

只有事情需要检查图像是否存在于正确的路径上

//在创建

    lstContact=new ArrayList<>();
    lstContact.add(new Contact("Andrea", "5874-2563", "Normal", R.drawable.user_painted_black64));
    lstContact.add(new Contact("Daniela", "2013-2365", "Normal", R.drawable.user_black));
    lstContact.add(new Contact("Juan", "7584-2323", "Normal", R.drawable.user_painted64));
    lstContact.add(new Contact("Elena", "7541-2110", "Normal", R.drawable.user_painted_black64));
    lstContact.add(new Contact("Maria", "7504-2000", "Normal", R.drawable.user_painted_white64_1));
    lstContact.add(new Contact("Edgardo", "2048-2048", "Normal", R.drawable.user_painted_white64_1));
    lstContact.add(new Contact("Alfonso", "9514-3214", "Normal", R.drawable.user_painted_black64));
    lstContact.add(new Contact("Erick", "5412-3652", "Normal", R.drawable.user_painted_white64_1));
    lstContact.add(new Contact("Andrea", "5874-2563", "Normal", R.drawable.user_painted_black64));
    lstContact.add(new Contact("Daniela", "2013-2365", "Normal", R.drawable.user_black));

    RecyclerView myrv = (RecyclerView) findViewById(R.id.id_recyclerView);
    RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this, lstContact);
    myrv.setLayoutManager(new GridLayoutManager(this, 3));
    myrv.setAdapter(myAdapter);

修订适配器类

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

    private Context mContext;
    private List<Contact> mData;

    public RecyclerViewAdapter(Context mContext, List<Contact> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view;
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        view = mInflater.inflate(R.layout.cardview_contact_layout, parent, false);
        return new MyViewHolder(view);

    }

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

        holder.tv_contact_name.setText(mData.get(position).getName());
        holder.img_contact_thumbnail.setImageResource(mData.get(position).getPhoto());
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
        pasando datos al detalle de contacto
                Intent intent = new Intent(mContext, Contact_details_activity.class);
                intent.putExtra("Nombre", mData.get(position).getNombre());
                intent.putExtra("Numero de Telefono", mData.get(position).getTelefono());
                intent.putExtra("Thumbnail", mData.get(position).getThumbnail());
                mContext.startActivity(intent);

            }
        });

        //Click Listener
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv_contact_name;
        ImageView img_contact_thumbnail;
        CardView cardView;

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

            tv_contact_name = (TextView) itemView.findViewById(R.id.id_contact_name);
            img_contact_thumbnail = (ImageView) itemView.findViewById(R.id.id_contact_img);
            cardView = (CardView) itemView.findViewById(R.id.id_cardview);
        }
    }
}