-1

I have a fragment that contains a recyclerview where I add an onclick listener to each item inside the Adapter like below

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

I'm getting the following runtime error everytime I click on one of the recyclerview items:

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
mazen el zoor
  • 305
  • 4
  • 16

2 Answers2

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>
Ferhat Ergün
  • 135
  • 1
  • 10
0

In Your Xml file, there must be Two Viewgroup inside your Scrollview.

As your error suggests.

ScrollView can host only one direct child

You have to make one Main Layout Inside your xml file and encapsulate them with Scrollview. just like this

<Scrollview>

    <LinearLayout>

   //your content

   </LinearLayout>

</Scrollview>
Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51