"IllegalStateException : scrollview can host only one direct child" in one LinearLayout adding
android, android-linearlayout, horizontalscrollview, illegalstateexception, textview
Solution
You just need to remove this line: `scroll.addView(layout);` You already have this declared in xml and are trying to add it again, that's why you are getting the multiple children exception.
Try to run 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);
}
}
Problem
I have list of attached files and want to make my LinearLayout with it be horizontally Scrollable. I add only one child LinearLayout to my HorizontalScrollView, besides I get 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 list of files to the LinearLayout in the ScrollView like 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); } scroll.addView(layout); } } ``` I add only one LinearLayout to the HorizontalScrollView, besides 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) ``` at the line ``` scroll.addView(layout); ```