E / RecyclerView:没有连接适配器;跳过布局

时间:2016-05-14 18:43:46

标签: javascript java android android-recyclerview

我是初学者,我正在尝试制作RSS Feed应用程序,但每次我尝试在我的模拟器或手机上启动我的应用程序时,它都会给我这些错误:

    05-14 18:25:17.750 2350-2350/? E/RecyclerView: No adapter attached; skipping layout
    05-14 18:25:17.916 2350-2350/? E/RecyclerView: No adapter attached; skipping layout
    05-14 18:25:18.342 2350-2350/com.gaming.labneh.rssreader I/Choreographer: Skipped 33 frames!  The application may be doing too much work on its main thread.
    05-14 18:25:18.356 2350-2350/com.gaming.labneh.rssreader E/RecyclerView: No adapter attached; skipping layout
    05-14 18:25:19.266 2350-2364/com.gaming.labneh.rssreader E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaaa23b20

以下是课程:

public class ReadRss extends AsyncTask<Void,Void,Void> {
Context context;
String address="https://www.sciencemag.org/rss/news_current.xml";
ProgressDialog progressDialog;
ArrayList<FeedItem>feedItems;
RecyclerView recyclerView;
URL url;
public ReadRss(Context context,RecyclerView recyclerView){
    this.recyclerView=recyclerView;
    this.context=context;
    progressDialog=new ProgressDialog(context);
    progressDialog.setMessage("Loading...");
}
@Override
protected void onPreExecute() {
    progressDialog.show();
    super.onPreExecute();
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    progressDialog.dismiss();
    MyAdapter adapter=new MyAdapter(context,feedItems);
    recyclerView.setLayoutManager(new LinearLayoutManager(context));
    recyclerView.setAdapter(adapter);


}

@Override
protected Void doInBackground(Void... params) {
    ProcessXml(Getdata());

    return null;
}

private void ProcessXml(Document data) {
    if (data != null) {
        feedItems=new ArrayList<>();
        Element root=data.getDocumentElement();
        Node channel =root.getChildNodes().item(1);
        NodeList items=channel.getChildNodes();
        for (int i=0;i<items.getLength();i++){
            Node cureentchild=items.item(1);
            if (cureentchild.getNodeName().equalsIgnoreCase("item")){
                FeedItem item=new FeedItem();
                NodeList itemchilds=cureentchild.getChildNodes();
                for (int j=0;j<itemchilds.getLength();j++){
                    Node cureent=itemchilds.item(j);
                    if (cureent.getNodeName().equalsIgnoreCase("title")) {
                        item.setTitle(cureent.getTextContent());
                    }else if (cureent.getNodeName().equalsIgnoreCase("description")){
                        item.setDescription(cureent.getTextContent());
                    }else if (cureent.getNodeName().equalsIgnoreCase("pubDate")){
                        item.setPubDate(cureent.getTextContent());
                    }else if (cureent.getNodeName().equalsIgnoreCase("link")) {
                        item.setLink(cureent.getTextContent());
                    }else if (cureent.getNodeName().equalsIgnoreCase("media:thumbnail")){
                        //this will return us thumbnail url
                        String url=cureent.getAttributes().item(0).getTextContent();
                        item.setThumbnailUrl(url);
            }
        }
                feedItems.add(item);
                Log.d("itemThumbnailUrl", item.getThumbnailUrl());
    }
}}}
public Document Getdata(){
    try {
        url=new URL(address);
        HttpURLConnection connection= (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        InputStream inputStream=connection.getInputStream();
        DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
        DocumentBuilder builder=builderFactory.newDocumentBuilder();
        Document xmlDoc= builder.parse(inputStream);
        return xmlDoc;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
  }
}

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

ArrayList<FeedItem>feedItems;
Context context;
public MyAdapter(Context context,ArrayList<FeedItem>feedItems){
    this.feedItems=feedItems;
    this.context=context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view= LayoutInflater.from(context).inflate(R.layout.custom_row_news_item,parent,false);
    MyViewHolder holder=new MyViewHolder(view);
    return holder;

}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    FeedItem current=feedItems.get(position);
    holder.Title.setText(current.getTitle());
    holder.Description.setText(current.getDescription());
    holder.Date.setText(current.getPubDate());
    Picasso.with(context).load(current.getThumbnailUrl()).into(holder.Thumbnail);

}

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

public class MyViewHolder extends RecyclerView.ViewHolder {
    TextView Title,Description,Date;
    ImageView Thumbnail;
    public MyViewHolder(View itemView) {
        super(itemView);
        Title= (TextView) itemView.findViewById(R.id.title_text);
        Description= (TextView) itemView.findViewById(R.id.description_text);
        Date= (TextView) itemView.findViewById(R.id.date_text);
        Thumbnail= (ImageView) itemView.findViewById(R.id.thumb_img);
    }
  }
}

我真的希望你能帮助我,谢谢!

修改

    public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    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();
        }
    });
    recyclerView= (RecyclerView) findViewById(R.id.recyclerview);
    ReadRss readRss=new ReadRss(this,recyclerView);
    readRss.execute();
}

@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);
 }
}

2 个答案:

答案 0 :(得分:0)

他们警告您还没有将适配器连接到您的回收站视图。

如果您在获取数据之前要做很长时间的工作。您应首先连接适配器,然后替换数据。

因此,您应首先将适配器附加到活动中:

MyAdapter adapter=new MyAdapter(context,feedItems);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(adapter);

然后当你获得onPostExecute()内的数据时;只需清除数据集,重新填充新数据集,然后只需通知适配器数据更改:

...onPostExecute(){
feedItems.clear();
feedItems.addAll(newFeedItems);
adapter.notifyDataSetChanged();
}

另一个注意事项:如果你的活动被破坏后你的asynctask继续运行;因为你的观点会被破坏,你会得到严重的崩溃。此外,将异步任务作为活动内部类保持可能会导致严重的内存泄漏。提供回调。

答案 1 :(得分:0)

我认为你的错误就在这里:

Node cureentchild=items.item(1);

将其更改为:

Node cureentchild=items.item(i);