Null Pointer Android

Asked

Viewed 56 times

0

While running the application, this Adapter error appears:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup)' on a null object reference.

By all accounts, the error is in the getView method, and probably in that part:

view = inflater.inflate(R.layout.list_notice, null);

Can someone help me?

My class Adapter:

public class NoticeAdapter extends BaseAdapter {

    private List<Notice> itemList;
    private LayoutInflater inflater;

    public NoticeAdapter(List<Notice> itemList, Context ctx) {
        this.itemList = itemList;
        inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        if (itemList != null)
            return itemList.size();
        return 0;
    }

    public Notice getItem(int position) {
        if (itemList != null)
            return itemList.get(position);
        return null;
    }

    public long getItemId(int position) {
        if (itemList != null)
            return itemList.get(position).hashCode();
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;

        if(view == null) {
            view = inflater.inflate(R.layout.list_notice, null);
            holder = new ViewHolder();
            view.setTag(holder);

            holder.tvId = (TextView) view.findViewById(R.id.title);
            holder.tvDesc = (TextView) view.findViewById(R.id.description);
            holder.tvPtime = (TextView) view.findViewById(R.id.publication_time);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.tvId.setText(itemList.get(position).getTitle());
        holder.tvDesc.setText(itemList.get(position).getDescription());
        holder.tvPtime.setText(itemList.get(position).getPublicationTime());

        return view;
    }

    private static class ViewHolder {
        TextView tvId;
        TextView tvDesc;
        TextView tvPtime;
    }

    public List<Notice> getItemList() {
        return itemList;
    }

    public void setItemList(List<Notice> itemList) {
        this.itemList = itemList;
    }

}
  • You are sure that the builder is being called, and that the call ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE) is returning a valid object (and not null)? I have no experience with Android, but your error message suggests that it is itself inflater which is null in value, so I would investigate if that’s what’s happening and, if it is, what may be the cause.

  • That’s right, @ramaral. Thank you!!

  • 1

    Change as well R.layout.list_notice for R.layout.item_layout the layout used here is the item and not where is the Listview.

  • @ramaral, could I ask another question about Fragment here, or I would have to open another question?

  • It depends on whether the question can be answered by comment or not. Ask and then see.

  • The list is loaded into the Fragment with the selection of the Actionbar menu item. However, when I click on a different tab than the one that has Fragment with the list, and return to the tab with the list, the list that was previously not destroyed. That is, a new request is made to the API, return the data normally and they are added to the list that was already there. You know how I can fix this?

  • I know. Ask nine questions and put the code Fragment.

  • @Now I’m curious: where is the answer?! Apparently you and the AP have been talking somewhere else, because between my first comment and the following, where the AP thanks you, there is nothing. After all, what really caused this NullPointerException?

  • 1

    @mgibsonbr, what caused Nullpointer, was I inflate the layout wrong, as the ramaral answered above :) kkkk

  • 1

    @mgibsonbr I made a comment following his that I deleted because it was not 100% correct. In it I said that the error was due to the exchange of layouts, which is not true because the error indicates that the inflater is null. When I realized that it was no longer possible to edit it, I decided to delete it and put another one. If you check between my pole and the AP there are 2s difference. I hope I made me understand.

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.