我无法在卡片视图名称和姓氏中设置“设置文本”

时间:2017-04-05 07:28:47

标签: java android android-recyclerview android-cardview

我在卡片视图中设置文字有问题。我有3名活动家。第1次活动是显示完成数据的编辑文本的列表,第2活动人员下次acttivty 3概要点击buton去MainActivty。单击MainActitvty时显示错误。

04-05 09:21:06.879 1035-1035/magdalena.pl.callmistake E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: magdalena.pl.callmistake, PID: 1035
                                                                        java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String magdalena.pl.callmistake.Person.getName()' on a null object reference
                                                                            at magdalena.pl.callmistake.PersonAdapter.onBindViewHolder(PersonAdapter.java:41)
                                                                            at magdalena.pl.callmistake.PersonAdapter.onBindViewHolder(PersonAdapter.java:18)
                                                                            at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6356)
                                                                            at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6389)
                                                                            at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5335)
                                                                            at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5598)

我的PersonAdapter

    public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.PersonViewHolder> {

        public List<Person> personList = new ArrayList<>();

        Person person;
        private LayoutInflater layoutInflater;

        public PersonAdapter(LayoutInflater layoutInflater) {
            this.layoutInflater = layoutInflater;
        }

        @Override
        public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.
                    from(parent.getContext()).inflate(R.layout.item_card, parent, false);

            return new PersonViewHolder(view);
        }

        @Override
        public void onBindViewHolder(PersonViewHolder holder, int position) {
            person = personList.get(position);

            holder.name.setText(person.getName());
            holder.surname.setText(person.getSurname());
        }

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

        public void addPerson(Person person) {

            int position = getItemCount();
            personList.add(position, person);
            notifyDataSetChanged();
        }


        public class PersonViewHolder extends RecyclerView.ViewHolder {


            public TextView name, surname;


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

                name = (TextView)itemView.findViewById(R.id.person_name);
                surname = (TextView)itemView.findViewById(R.id.person_surname);
            }



        }
    }

class Person 

public class Person implements Parcelable
{

    private String name;
    private String surname;
    private String email;
    private String phone;
    private String description;

    protected Person(Parcel in) {
        name = in.readString();
        surname = in.readString();
        email = in.readString();
        phone = in.readString();
        description = in.readString();
    }


    public Person(String name, String surname, String email, String phone, String description) {
        this.name = name;
        this.surname = surname;
        this.email = email;
        this.phone = phone;
        this.description = description;
    }

    public String setName(String name) {
        this.name = name;
        return null;
    }

    public String getSurname() {
        return surname;
    }

    public String setSurname(String surname) {
        this.surname = surname;
        return null;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public static Creator<Person> getCREATOR() {
        return CREATOR;
    }

    public static final Creator<Person> CREATOR = new Creator<Person>() {
        @Override
        public Person createFromParcel(Parcel in) {
            return new Person(in);
        }

        @Override
        public Person[] newArray(int size) {
            return new Person[size];
        }
    };

    public String getName() {
        return name;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(name);
        parcel.writeString(surname);
        parcel.writeString(email);
        parcel.writeString(phone);
        parcel.writeString(description);
    }
}

有什么问题?

所有代码 https://github.com/Madzia123/CallInert

5 个答案:

答案 0 :(得分:0)

显然,在onBindViewHolder方法中,person对象为null。并且您尝试在null引用上调用getName()方法。所以它导致错误。确保对Person对象有有效的引用。

答案 1 :(得分:0)

您正在将人员添加到

的personList
public void addPerson(Person person) {
    int position = getItemCount();
    personList.add(position, person);
    notifyDataSetChanged();
}

这就是你打电话的原因

person = personList.get(position);

返回null

试试这个

public void addPerson(Person person) {
    if(person==null){
      Log.e("error","person is null");
      return;
    }
    int position = getItemCount();
    personList.add(position, person);
    notifyDataSetChanged();
}

答案 2 :(得分:0)

final Person person = personList.get(position);
holder.name.setText(person.getName());

答案 3 :(得分:0)

从附件logs开始,您似乎从适配器的NullPointerException拨打Person.getName()时收到onBindViewHolder()

  

java.lang.NullPointerException:尝试调用虚方法   'null。上的java.lang.String magdalena.pl.callmistake.Person.getName()'   对象参考

<强>解决方案:

更新PersonAdapter,如下所示:

public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.PersonViewHolder> {

    Context context;
    private LayoutInflater layoutInflater;

    // Person List
    public List<Person> personList;

    public PersonAdapter(Context context, List<Person> persons) {
        this.context = context;
        this.personList = persons;

        layoutInflater= LayoutInflater.from(context);
    }

    @Override
    public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_card, parent, false);

        // View holder
        PersonViewHolder holder = new PersonViewHolder(view);

        return holder;
    }

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

        // Person
        Person person = personList.get(position);

        holder.name.setText(person.getName());
        holder.surname.setText(person.getSurname());
    }

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

    public void addPerson(Person person) {

        int position = getItemCount();
        personList.add(position, person);
        notifyDataSetChanged();
    }


    public class PersonViewHolder extends RecyclerView.ViewHolder {

        public TextView name, surname;

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

            name = (TextView) itemView.findViewById(R.id.person_name);
            surname = (TextView) itemView.findViewById(R.id.person_surname);
        }
    }
}

Activity执行此操作:

..........
.................
Context mContext;

// RecyclerView
RecyclerView recyclerView;
PersonAdapter personAdapter;
RecyclerView.LayoutManager layoutManager;

// Values
List<Person> listPerson;

@Override
protected void onCreate(Bundle savedInstanceState) {
    .......
    ............

    // Context
    mContext = this;

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    // Set your recyclerView  layout manager

    // Person List
    listPerson = new ArrayList<Person>();

    // Add some person data to list from API call or from Database
    // Here I added some for test purpose
    listPerson.add(new Person("Hello Android", "Android", "google@gmail.com", "21132342423", "Lorem ipsum"));
    listPerson.add(new Person("Hello Android", "Android", "google@gmail.com", "21132342423", "Lorem ipsum"));


    // specify an adapter
    personAdapter = new PersonAdapter(mContext, listPerson);
    recyclerView.setAdapter(personAdapter);
}

...............
...........................

希望这会对你有帮助〜

答案 4 :(得分:-2)

首先传递您调用此适配器的数据。

adapter = new PersonAdapter(getContext(),(ArrayList<Person>) list);

使用arraylist.method访问类方法。

 holder.name.setText(person.personList(position).getName());