【问题标题】:App crashes when trying to open activity inside recyclerview that's inside a fragment [duplicate]尝试在片段内的 recyclerview 中打开活动时,应用程序崩溃 [重复]
【发布时间】: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


【解决方案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>

【讨论】:

    【解决方案2】:

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

    正如您的错误所暗示的那样。

    ScrollView 只能承载一个直接子节点

    您必须在您的 xml 文件中创建一个主布局并使用 Scrollview 封装它们。就这样

    <Scrollview>
    
        <LinearLayout>
    
       //your content
    
       </LinearLayout>
    
    </Scrollview>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-07
      • 1970-01-01
      • 1970-01-01
      • 2021-02-28
      相关资源
      最近更新 更多