2

I keep getting this error whenever I start the activity.

Here's the complete stack trace:

Process: com.example, PID: 28799
                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.activity.EditProjectActivity}: java.lang.IllegalStateException: ScrollView can host only one direct child
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2689)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2754)
                                                                    at android.app.ActivityThread.access$900(ActivityThread.java:177)
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                    at android.os.Looper.loop(Looper.java:145)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5938)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
                                                                 Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child
                                                                    at android.widget.ScrollView.addView(ScrollView.java:416)
                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:822)
                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
                                                                    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
                                                                    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                    at com.example.activity.EditProjectActivity.onCreate(EditProjectActivity.java:38)
                                                                    at android.app.Activity.performCreate(Activity.java:6288)
                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2642)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2754) 
                                                                    at android.app.ActivityThread.access$900(ActivityThread.java:177) 
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                    at android.os.Looper.loop(Looper.java:145) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5938) 
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 

And this is the layout file

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".activity.EditProjectActivity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    >
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:orientation="horizontal"
                    android:id="@+id/ep_layout_name"
                    >
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Project Name: "/>
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:id="@+id/ep_edit_name"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/ep_layout_name"
                    android:layout_marginTop="20dp"
                    android:layout_centerHorizontal="true"
                    android:orientation="vertical"
                    android:id="@+id/ep_layout_categories"
                    >
                    <ListView
                        android:id="@+id/ep_list_categories"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        />
                    <Button
                        android:id="@+id/ep_btn_add_category"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="Add"
                        android:onClick="onAddCategoryButtonClick"
                        />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/ep_layout_categories"
                    android:layout_marginTop="20dp"
                    android:layout_centerHorizontal="true"
                    android:orientation="vertical"
                    android:id="@+id/ep_layout_versions"
                    >
                    <ListView
                        android:id="@+id/ep_list_versions"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        />
                    <Button
                        android:id="@+id/ep_btn_add_version"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="Add"
                        android:onClick="onAddVersionButtonClick"
                        />
                </LinearLayout>
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

I understand what the error means, and I've tried to solve it accordingly, but it still throws out the same exception.

The ScrollView has only one direct child and that is the RelativeLayout. I've also made it so that the ScrollView has a parent and is not the base view in case it was causing the exception but it didn't work.

Amir
  • 16,067
  • 10
  • 80
  • 119
Alex Ferg
  • 801
  • 1
  • 9
  • 17

3 Answers3

3

May be try to add a LinearLayout again with a vertical orientation before a RelativeLayout

  <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".activity.EditProjectActivity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    >
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
<LinearLayout
                    android:layout_width="match_parent"
                    android:orientation="vertical"
                    android:layout_height="wrap_content">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:orientation="horizontal"
                    android:id="@+id/ep_layout_name"
                    >
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Project Name: "/>
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:id="@+id/ep_edit_name"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/ep_layout_name"
                    android:layout_marginTop="20dp"
                    android:layout_centerHorizontal="true"
                    android:orientation="vertical"
                    android:id="@+id/ep_layout_categories"
                    >
                    <ListView
                        android:id="@+id/ep_list_categories"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        />
                    <Button
                        android:id="@+id/ep_btn_add_category"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="Add"
                        android:onClick="onAddCategoryButtonClick"
                        />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/ep_layout_categories"
                    android:layout_marginTop="20dp"
                    android:layout_centerHorizontal="true"
                    android:orientation="vertical"
                    android:id="@+id/ep_layout_versions"
                    >
                    <ListView
                        android:id="@+id/ep_list_versions"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        />
                    <Button
                        android:id="@+id/ep_btn_add_version"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="Add"
                        android:onClick="onAddVersionButtonClick"
                        />
                </LinearLayout>
         </LinearLayout>
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>
Lutaaya Huzaifah Idris
  • 3,596
  • 8
  • 38
  • 77
3

Do focus

  1. If you are using ScrollView then it should contain only child layout and that child can contain multiple child layout.
  2. Scroll view should not contain parallel layout, if you have parallel layout then you can keep into single parent layout, I am using Linear layout and by using above explain approach working fine.

Attaching screen shot for your reference....

See in image define layout within scroll view

Sheriff
  • 738
  • 10
  • 20
1

You might be adding some view to scrollview in your java file. So don't add any view to scrollview.

scrollview.addView(someView); // Remove any code like this