简体   繁体   English

java.lang.IllegalStateException: Horizo​​ntalScrollView 只能承载一个直接子级

[英]java.lang.IllegalStateException: HorizontalScrollView can host only one direct child

I want to realize the Gallery using HorizontalScrollView.我想使用 Horizo​​ntalScrollView 来实现 Gallery。 But it throws java.lang.illegalStateException.但它会抛出 java.lang.illegalStateException。 The logcat hint that the HorizontalScrollView can host only one direct child. logcat 提示 Horizo​​ntalScrollView 只能承载一个直接子级。 I only set one direct child - LinearLayout to it.我只给它设置了一个直接子元素——LinearLayout。 I searched from the Internet, but haven't solved the problem.我从网上搜索,但没有解决问题。 Hope anyone can help me.希望任何人都可以帮助我。 Belows are the code.下面是代码。

xml xml

<HorizontalScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:scrollbars="none"
    >

    <LinearLayout
       android:id="@+id/gallery_layout"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="horizontal">
    </LinearLayout>

</HorizontalScrollView>

item xml项目xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="10dp"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingBottom="20dp"
    >

    <ImageView
        android:id="@+id/trend_item_imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="8dp"
        android:src="@drawable/gallery0"
        />

    <TextView
        android:id="@+id/trend_item_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="权利的游戏"
        android:textSize="16sp"
        />

</LinearLayout>

Fragment分段

public class DiscoverFragment extends Fragment {

    private int[] imageResources = {R.drawable.gallery0, R.drawable.gallery1, R.drawable.gallery2,
        R.drawable.gallery3, R.drawable.gallery4, R.drawable.gallery5};

    private String[] imageDescriptions = {"权利的游戏", "风吹的风景", "插画背景", "美食君", "吃", "你好四月"};

    private List<TrendItem> trendItemList;
    private LinearLayout galleryLayout;

    public DiscoverFragment() {
    // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_discover, container, false);
        ButterKnife.bind(this, view);
        galleryLayout = ButterKnife.findById(view, R.id.gallery_layout);
        initData();
        setView();
        return view;
    }

    private void initData(){
        trendItemList = new ArrayList<>();
        for (int i = 0; i < imageResources.length; i++){
            TrendItem trendItem = new TrendItem();
            trendItem.setImageResource(imageResources[i]);
            trendItem.setText(imageDescriptions[i]);
            trendItemList.add(trendItem);
        }
    }

    private void setView(){
        for (int i = 0; i < trendItemList.size(); i++){
            LayoutInflater inflater = LayoutInflater.from(getContext());
            View view = inflater.inflate(R.layout.trend_item, galleryLayout, false);
            ImageView trendItemImageView = ButterKnife.findById(view, R.id.trend_item_imageview);
            TextView trendItemTextView = ButterKnife.findById(view, R.id.trend_item_textview);
            trendItemImageView.setImageResource(trendItemList.get(i).getImageResource());
            trendItemTextView.setText(trendItemList.get(i).getText());
            galleryLayout.addView(view);
        }
    }
}

Meaning, you have to add the ImageView , TextView to the Linearlayout .意思是,您必须将ImageViewTextView添加到Linearlayout when you add the imageview you are adding it to the HorizontalScrollview which also has a LinearLayout in it their by adding 2 child elements to the HorizontalScrollView which you cannot do.当您添加 imageview 时,您将它添加到HorizontalScrollview ,该LinearLayout中也有一个LinearLayout ,通过向 Horizo​​ntalScrollView 添加 2 个子元素,这是您无法做到的。

<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:scrollbars="none">

<LinearLayout
   android:id="@+id/gallery_layout"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="horizontal">

<ImageView
    android:id="@+id/trend_item_imageview"
    android:layout_width="your_size"
    android:layout_height="your_size"
    android:src="@drawable/gallery0"/>

<TextView
    android:id="@+id/trend_item_textview"
    android:layout_width="your_size"
    android:layout_height="your_size"
    android:text="权利的游戏"
    android:textSize="16sp"/>

</LinearLayout>

</HorizontalScrollView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果互联网关闭,则出现错误 java.lang.IllegalStateException: ScrollView can host only one direct child - If internet is off getting error java.lang.IllegalStateException: ScrollView can host only one direct child Horizo​​ntalScrollView只能托管一个直接子项 - HorizontalScrollView can host only one direct child java.lang.IllegalStateException:目标主机为null - java.lang.IllegalStateException: Target host is null RxJava:“java.lang.IllegalStateException:只允许一个订阅者!” - RxJava: “java.lang.IllegalStateException: Only one subscriber allowed!” 如何解决 java.lang.IllegalStateException:没有主机? - How to solve java.lang.IllegalStateException: No host? java.lang.IllegalStateException:页面只能偏移一个正数 - java.lang.IllegalStateException: Page can only be offset by a positive amount java.lang.IllegalStateException:启动孩子时出错 - java.lang.IllegalStateException: Error starting child 获取java.lang.IllegalStateException:用于子视图 - Getting java.lang.IllegalStateException: for child view 当只有一个直接子项时,Java返回“ScrollView只能托管一个直接子项” - Java returns “ScrollView can host only one direct child” when there's only one direct child java.lang.IllegalStateException - java.lang.IllegalStateException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM