• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

No adapter attached; skipping layout error

I am facing No adapter attached; skipping layout error and app crash.

Can you please help me. Thanks.

Error:

10-26 23:06:36.015 11253-11253/? E/RecyclerView: No adapter attached; skipping layout

@override
public int getItemCount(){
return rssObject.items.size(); >>> FeedAdapter.java:97
}

Exception:

10-26 23:06:37.138 11253-11253/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.news.lstn, PID: 11253
java.lang.NullPointerException: Attempt to read from field 'java.util.List com.news.lstn.Model.RSSObject.items' on a null object reference
at com.news.lstn.Adapter.FeedAdapter.getItemCount(FeedAdapter.java:97)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3493)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3019)
at android.view.View.measure(View.java:17637)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:482)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17637)
 
FeedAdapter Class:

class FeedViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener{

public TextView txtTitle, txtPubDate, txtContent;
private ItemClickListener itemClickListener;


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

txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
txtPubDate = (TextView) itemView.findViewById(R.id.txtPubDate);
txtContent = (TextView) itemView.findViewById(R.id.txtContent);

//Set Event
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);

}

public ItemClickListener getItemClickListener() {
return itemClickListener;
}

public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}

@override
public void onClick(View v) {
itemClickListener.onClick(v,getAdapterPosition(), false);

}

@override
public boolean onLongClick(View v) {
itemClickListener.onClick(v,getAdapterPosition(), true);
return true;
}
}


public class FeedAdapter extends RecyclerView.Adapter<FeedViewHolder> {

private RSSObject rssObject;
private Context mContext;
private LayoutInflater inflater;

public FeedAdapter(RSSObject rssObject, Context mContext) {
this.rssObject = rssObject;
this.mContext = mContext;
inflater = LayoutInflater.from(mContext);
}

@override
public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = inflater.inflate(R.layout.row,parent,false);
return new FeedViewHolder(itemView);
}

@override
public void onBindViewHolder(FeedViewHolder holder, int position){
holder.txtTitle.setText(rssObject.getItems().get(position).getTitle());
holder.txtPubDate.setText(rssObject.getItems().get(position).getPubDate());
holder.txtContent.setText(rssObject.getItems().get(position).getContent());

holder.setItemClickListener(new ItemClickListener() {
@override
public void onClick(View view, int position, boolean isLongClick) {
if (!isLongClick){
//Intent intent = new Intent(context,webviewClass.class);
//intent.putExtra("RSS_Link",rssObject.getItems().get(posistion).getLink());
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(rssObject.getItems().get(position).getLink()));
browserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
mContext.startActivity(browserIntent);
}
}
});

}
@override
public int getItemCount(){
return rssObject.items.size();

}
}
RssObject Class

public class RSSObject
{
public String status;
public Feed feed;
public List<Item> items;

public RSSObject(String status, Feed feed, List<Item> items) {
this.status = status;
this.feed = feed;
this.items = items;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public Feed getFeed() {
return feed;
}

public void setFeed(Feed feed) {
this.feed = feed;
}

public List<Item> getItems() {
return items;
}

public void setItems(List<Item> items) {
this.items = items;
}
}

Item Class

public class Item
{
public String title;
public String pubDate;
public String link;
public String guid;
public String author;
public String thumbnail;
public String description;
public String content;
public Object enclosure;
public List<String> categories;

public Item(String title, String pubDate, String link, String guid, String author, String thumbnail, String description, String content, Object enclosure, List<String> categories) {
this.title = title;
this.pubDate = pubDate;
this.link = link;
this.guid = guid;
this.author = author;
this.thumbnail = thumbnail;
this.description = description;
this.content = content;
this.enclosure = enclosure;
this.categories = categories;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getPubDate() {
return pubDate;
}

public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}

public String getLink() {
return link;
}

public void setLink(String link) {
this.link = link;
}

public String getGuid() {
return guid;
}

public void setGuid(String guid) {
this.guid = guid;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getThumbnail() {
return thumbnail;
}

public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}

public String getDescription() {
return description;
}

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

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public Object getEnclosure() {
return enclosure;
}

public void setEnclosure(Object enclosure) {
this.enclosure = enclosure;
}

public List<String> getCategories() {
return categories;
}

public void setCategories(List<String> categories) {
this.categories = categories;
}
}

 
You're code is difficult to read. Please format it correctly, with indentation, and use [code][/code] tags.
Also please indicate which line is line 97 in the FeedAdapter class.
 
Feed Adapter Class

Code:
class FeedViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener{

    public TextView txtTitle, txtPubDate, txtContent;
    private ItemClickListener itemClickListener;


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

        txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
        txtPubDate = (TextView) itemView.findViewById(R.id.txtPubDate);
        txtContent = (TextView) itemView.findViewById(R.id.txtContent);

        //Set Event
        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);

    }

    public ItemClickListener getItemClickListener() {
        return itemClickListener;
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    [USER=1021285]@override[/USER]
    public void onClick(View v) {
        itemClickListener.onClick(v,getAdapterPosition(), false);

    }

    [USER=1021285]@override[/USER]
    public boolean onLongClick(View v) {
        itemClickListener.onClick(v,getAdapterPosition(), true);
        return true;
    }
}


public class FeedAdapter extends RecyclerView.Adapter<FeedViewHolder> {

    private RSSObject rssObject;
    private Context mContext;
    private LayoutInflater inflater;

    public FeedAdapter(RSSObject rssObject, Context mContext) {
        this.rssObject = rssObject;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
    }

    [USER=1021285]@override[/USER]
    public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.row,parent,false);
        return new FeedViewHolder(itemView);
    }

    [USER=1021285]@override[/USER]
    public void onBindViewHolder(FeedViewHolder holder, int position){
        holder.txtTitle.setText(rssObject.getItems().get(position).getTitle());
        holder.txtPubDate.setText(rssObject.getItems().get(position).getPubDate());
        holder.txtContent.setText(rssObject.getItems().get(position).getContent());

        holder.setItemClickListener(new ItemClickListener() {
            [USER=1021285]@override[/USER]
            public void onClick(View view, int position, boolean isLongClick) {
                if (!isLongClick){
                    //Intent intent = new Intent(context,webviewClass.class);
                    //intent.putExtra("RSS_Link",rssObject.getItems().get(posistion).getLink());
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(rssObject.getItems().get(position).getLink()));
                    browserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                    mContext.startActivity(browserIntent);
                }
            }
        });

    }
    [USER=1021285]@override[/USER]
    public int getItemCount(){
        return rssObject.items.size(); //FeedAdapter.java:97

    }
}

RSSObject Class

Code:
public class RSSObject
{
    public String status;
    public Feed feed;
    public List<Item> items;

    public RSSObject(String status, Feed feed, List<Item> items) {
        this.status = status;
        this.feed = feed;
        this.items = items;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Feed getFeed() {
        return feed;
    }

    public void setFeed(Feed feed) {
        this.feed = feed;
    }

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }
}
 
Ok so the exception was

Code:
ava.lang.NullPointerException: Attempt to read from field 'java.util.List com.news.lstn.Model.RSSObject.items' on a null object reference
at com.news.lstn.Adapter.FeedAdapter.getItemCount(FeedAdapter.java:97)

So I'm guessing the offending line in your code is

Code:
public int getItemCount() {
 return rssObject.items.size();  <--------------------- rssObject is null at this point
}

This means that rssObject has a null value, which probably means that the value you passed in to the constructor of this class was null. You should debug this further by looking at where you create the adapter, and look at the variables you're passing in.
 
Back
Top Bottom