"IllegalStateException: scrollview can contain only one direct child" in one add LinearLayout

I have a list of file attachments and want my LinearLayout with it to be horizontally scrollable. I add only one child LinearLayout to my HorizontalScrollView, in addition, I get an IllegalStateException. My xml:

<HorizontalScrollView android:id="@+id/scrollMessageFiles" android:layout_width="fill_parent" android:layout_height="65dp" android:layout_below="@+id/editMessage" android:orientation="horizontal" android:weightSum="1.0" > <LinearLayout android:id="@+id/panelMessageFiles" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#FFFFFF" > </LinearLayout> </HorizontalScrollView> 

and want to add a list of files to LinearLayout in ScrollView, for example:

 public void addFiles() { HorizontalScrollView scroll = (HorizontalScrollView) findViewById(R.id.scrollMessageFiles); LinearLayout layout = (LinearLayout) findViewById(R.id.panelMessageFiles); if(!FileManagerActivity.getFinalAttachFiles().isEmpty()) { for (File file: FileManagerActivity.getFinalAttachFiles()) { View line = new View(this); line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT)); line.setBackgroundColor(0xAA345556); informationView = new TextView(this); informationView.setTextColor(Color.BLACK); informationView.setTextSize(12); informationView.setCompoundDrawablesWithIntrinsicBounds( 0, R.drawable.file_icon, 0, 0); informationView.setText(file.getName().toString()); layout.addView(informationView, 0); layout.addView(line, 1); } scroll.addView(layout); } } 

I add only one LinearLayout to the HorizontalScrollView, in addition, I get

  FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.assignmentexpert/com.assignmentexpert.NewMessageActivity}: java.lang.IllegalStateException: HorizontalScrollView can host only one direct child at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) at android.app.ActivityThread.access$1500(ActivityThread.java:117) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:3687) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.IllegalStateException: HorizontalScrollView can host only one direct child at android.widget.HorizontalScrollView.addView(HorizontalScrollView.java:223) at com.assignmentexpert.NewMessageActivity.addFiles(NewMessageActivity.java:165) at com.assignmentexpert.NewMessageActivity.onCreate(NewMessageActivity.java:90) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 

in line

  scroll.addView(layout); 
+4
source share
6 answers

You just need to remove this line: scroll.addView(layout); You already have this declared in xml and you are trying to add it again, so you get an exception from several children.

Try running this:

 public void addFiles() { HorizontalScrollView scroll = (HorizontalScrollView) findViewById(R.id.scrollMessageFiles); LinearLayout layout = (LinearLayout) findViewById(R.id.panelMessageFiles); if(!FileManagerActivity.getFinalAttachFiles().isEmpty()) { for (File file: FileManagerActivity.getFinalAttachFiles()) { View line = new View(this); line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT)); line.setBackgroundColor(0xAA345556); informationView = new TextView(this); informationView.setTextColor(Color.BLACK); informationView.setTextSize(12); informationView.setCompoundDrawablesWithIntrinsicBounds( 0, R.drawable.file_icon, 0, 0); informationView.setText(file.getName().toString()); layout.addView(informationView, 0); layout.addView(line, 1); } // This line is telling the system to add your LinearLayout to the ScrollView when it is already there, declared in your xml layout file //scroll.addView(layout); } } 
+5
source

I decided to just remove scroll.addView(layout);

+3
source
 <HorizontalScrollView android:id="@+id/btnholder" android:layout_width="fill_parent" android:layout_height="50dp" android:layout_marginTop="131dp" android:scrollbars="horizontal" > <LinearLayout android:id="@+id/holderscroll" android:layout_width="fill_parent" android:layout_height="55dp" android:layout_marginTop="2dp" > </LinearLayout> </HorizontalScrollView> 

subcategoryscrollV = (HorizontalScrollView) findViewById (R.id.btnholder);

addbtnlingleay = (LinearLayout) findViewById (R.id.holderscroll);

And add your button to the layout:

addbtnlinearlay.addView (BTN [I]);

+3
source

Sorry, but I didn’t even bother to read your code, the error is very clear:

HorizontalScrollView can host only one direct child

Do not put more than one child in the form of a horizontal scroll! place the group view there (linear layout, relative layout, whatever you want), and you put the children in this group.

+2
source

Remove the old child before trying to add a new one. Try:

 scroll.removeAllViews(); 

front

 scroll.addView(layout); 
+2
source

ScrollView can have only one child, so it makes no sense to add more children to it. Lets say your ScrollView has a LinearLayout inside it, then you can add more images to LinearLayout:

+2
source

Source: https://habr.com/ru/post/1442342/


All Articles