【发布时间】:2018-10-01 01:09:54
【问题描述】:
我有一个包含 recyclerview 的片段,我在其中为适配器内的每个项目添加了一个 onclick 侦听器,如下所示
public void onBindViewHolder(CardViewHolder holder, int position) {
// Get the current news item at the position
final News currentItem = mNewsList.get(position);
// Get the news title information from the current news item and
// set text on the news title {@link TextView}
holder.newsTitleTextView.setText(currentItem.getTitle());
// Get the news section information from the current news item
// and set text on the section {@link TextView}
holder.sectionNameTextView.setText(currentItem.getSection());
// Get the published date of the current news item information from the current news item
// and set the same as text on the date published {@link TextView}
holder.datePublishedTextView.setText(currentItem.getTestDate());
// Register and setup listener to open up news story in web browser
holder.storyCard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Parse string as url object
//Uri webpage = Uri.parse(currentItem.getStoryUrl());
Intent i = new Intent(v.getContext().getApplicationContext(), NewsReader.class);
i.putExtra("image", currentItem.getThumbnail());
i.putExtra("title", currentItem.getTitle());
i.putExtra("body", currentItem.getSection());
i.putExtra("date", currentItem.getTestDate());
i.putExtra("url", currentItem.getStoryUrl());
v.getContext().getApplicationContext().startActivity(i);
}
});
// Check whether or not the current news item has a thumbnail or not
if (currentItem.getThumbnail() == null) {
// The current news item does not have thumbnail information
// Set scale type for the default image
holder.newsThumbnail.setScaleType(ImageView.ScaleType.CENTER);
// Set the default image on the {@link ImageView} for the thumbnail
holder.newsThumbnail.setImageResource(R.drawable.no_thumbnail);
} else {
// The current news item has thumbnail information
holder.newsThumbnail.setScaleType(ImageView.ScaleType.CENTER_CROP);
// Get the bitmap thumbnail from the current news item and
// Set it as the image on the {@link ImageView} thumbnail
holder.newsThumbnail.setImageBitmap(currentItem.getThumbnail());
}
}
每次单击其中一个 recyclerview 项目时,我都会收到以下运行时错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.thenews/com.example.android.thenews.NewsReader}: android.view.InflateException: Binary XML file line #0: ScrollView can host only one direct child
【问题讨论】:
-
我不太明白你刚才说什么@NileshRathod
-
它可能与您的布局有关
标签: java android android-recyclerview crash