当试图打开片段内的recyclerview内的活动时,应用程序崩溃

时间:2018-04-20 10:27:55

标签: java android android-recyclerview crash

我有一个包含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

2 个答案:

答案 0 :(得分:1)

<ScrollView .....>

//if you want to add some layout it has to have one body so you need to place in one body like this
    <LinearLayout..>
        <...Some Layouts .../>
    </LinearLayout>

</ScrollView>

答案 1 :(得分:0)

在您的Xml文件中, Scrollview 中必须有两个Viewgroup

正如您的错误所示。

  

ScrollView只能托管一个直接孩子

您必须在xml文件中创建一个主要布局,并使用Scrollview对其进行封装。就像这样

<Scrollview>

    <LinearLayout>

   //your content

   </LinearLayout>

</Scrollview>