をスキップし、私はRSSフィードアプリを作るためにしようとしているが、私は私のエミュレータや携帯電話で自分のアプリを起動しようとするたびに、それは私にこれらのエラー(複数可)を提供します:E/RecyclerView:アダプターは接続されていません。私は初心者だレイアウト
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);
}
}
}
私は本当にあなたが私を助けることを願って、ありがとう!
EDIT:
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);
}
}
あなたは 'ReadRss'を実行し、私はポストを更新しました' RecycleView' –
よしを渡す場所を投稿してください。 @ shayanpourvatan –