Reputation: 1927
I have been getting this "ScrollView can host only one direct child" even though I am only using one main layout in the layout XML code. Basically what I'm trying to do is call/start the RegisterFragment from the LoginFragment. I am trying to do that using the following code:
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.vvLoginBanner: toggleVolume(); break;
case R.id.btnLogin: login(); break;
case R.id.tvSignup: FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.loginFragmentLayout, new RegisterFragment());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
break;
}
}
Here is the code for both of these XML file that could be possibly causing the error:
LoginFragment.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/loginFragmentContainer"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="true"
tools:context="com.bvmobileapps.bvmobileapps.LoginFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:id="@+id/flBannerProgressBarWrapper"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:background="@color/black"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/pbBanner"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"/>
</FrameLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/nsvScrollView"
android:layout_below="@id/rlVideoWrapper"
android:isScrollContainer="false"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/loginFragmentLayout"
android:isScrollContainer="true"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:id="@+id/fragmentFrameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="@+id/llBannerWrapper"
android:weightSum="100"
android:orientation="horizontal"
android:layout_marginTop="@dimen/inter_item_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_weight="10"
android:layout_width="0dp"
android:layout_height="1px"/>
<ImageView
android:id="@+id/ivBanner"
android:src="@drawable/bv_banner"
android:layout_weight="80"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_marginStart="@dimen/inter_item_margin"
android:layout_marginEnd="@dimen/inter_item_margin"
android:layout_marginBottom="@dimen/intra_item_margin"
android:layout_marginTop="@dimen/inter_item_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tvUsername"
android:hint="@string/username_hint"
android:singleLine="true"
android:maxLength="20"
android:imeOptions="actionNext"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_marginStart="@dimen/inter_item_margin"
android:layout_marginEnd="@dimen/inter_item_margin"
android:layout_marginTop="@dimen/intra_item_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tvPassword"
android:hint="@string/password_hint"
android:fontFamily="@font/segoe_ui_light"
android:maxLength="20"
android:singleLine="true"
android:imeOptions="actionDone"
android:inputType="textPassword"
android:gravity="start"
android:ellipsize="start"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:layout_margin="@dimen/inter_item_margin"
android:fontFamily="@font/segoe_ui_semi_bold"
android:text="@string/login_btn"
app:backgroundTint="@color/bv_blue" />
<ProgressBar
android:id="@+id/pbLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-14dp"
android:layout_marginBottom="@dimen/inter_item_margin"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/tvSignup"
android:text="@string/signup"
android:textSize="@dimen/smallest_text"
android:textColor="@color/bv_blue"
android:textAlignment="center"
android:layout_marginRight="20dp"
android:layout_width="150dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tvForgotPassword"
android:text="@string/forgot_password"
android:textSize="@dimen/smallest_text"
android:textColor="@color/bv_blue"
android:textAlignment="center"
android:layout_marginLeft="0dp"
android:layout_width="150dp"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</RelativeLayout>
RegisterFragment.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/registerFragmentContainer"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="true"
tools:context="com.bvmobileapps.bvmobileapps.RegisterFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:id="@+id/flBannerProgressBarWrapper"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:background="@color/black"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/pbBanner"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"/>
</FrameLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/nsvScrollView"
android:layout_below="@id/rlVideoWrapper"
android:isScrollContainer="false"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:isScrollContainer="true"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/registerFragmentLayout"
android:layout_marginStart="@dimen/inter_item_margin"
android:layout_marginEnd="@dimen/inter_item_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:gravity="center_horizontal"
android:weightSum="10"
android:orientation="horizontal">
<ImageView
android:id="@+id/ivValidUsername"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="21dp"
android:layout_marginBottom="@dimen/intra_item_margin"
android:scaleType="fitXY"
android:src="@drawable/red_error" />
<com.google.android.material.textfield.TextInputLayout
android:layout_marginStart="@dimen/inter_item_margin"
android:layout_marginEnd="@dimen/inter_item_margin"
android:layout_marginBottom="@dimen/intra_item_margin"
android:layout_marginTop="@dimen/inter_item_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tvUsername"
android:hint="@string/username_hint"
android:singleLine="true"
android:maxLength="20"
android:layout_marginLeft="@dimen/inter_item_margin_left"
android:imeOptions="actionNext"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
</RelativeLayout>
<include layout="@layout/horizontal_divider"
android:id="@+id/source_divider"
android:layout_marginTop="@dimen/intra_item_margin"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintTop_toBottomOf="@id/photo_source_edit_text_wrapper"/>
<RelativeLayout
android:layout_marginStart="@dimen/inter_item_margin"
android:layout_marginEnd="@dimen/inter_item_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:gravity="center_horizontal"
android:weightSum="10"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/ivValidEmail"
android:layout_gravity="center_horizontal"
android:src="@drawable/red_error"
android:scaleType="fitXY"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_marginStart="@dimen/inter_item_margin"
android:layout_marginEnd="@dimen/inter_item_margin"
android:layout_marginTop="@dimen/intra_item_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tvEmail"
android:hint="@string/email_hint"
android:fontFamily="@font/segoe_ui_light"
android:maxLength="20"
android:singleLine="true"
android:imeOptions="actionDone"
android:inputType="text"
android:gravity="start"
android:ellipsize="start"
android:layout_marginLeft="@dimen/inter_item_margin_left"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
</RelativeLayout>
<include layout="@layout/horizontal_divider"
android:id="@+id/source_divider"
android:layout_marginTop="@dimen/intra_item_margin"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintTop_toBottomOf="@id/photo_source_edit_text_wrapper"/>
<RelativeLayout
android:layout_marginStart="@dimen/inter_item_margin"
android:layout_marginEnd="@dimen/inter_item_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:gravity="center_horizontal"
android:weightSum="10"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/ivValidPassword"
android:layout_gravity="center_horizontal"
android:src="@drawable/red_error"
android:scaleType="fitXY"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_marginStart="@dimen/inter_item_margin"
android:layout_marginEnd="@dimen/inter_item_margin"
android:layout_marginTop="@dimen/intra_item_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tvPassword"
android:hint="@string/password_hint"
android:fontFamily="@font/segoe_ui_light"
android:maxLength="20"
android:singleLine="true"
android:imeOptions="actionDone"
android:inputType="textPassword"
android:gravity="start"
android:ellipsize="start"
android:layout_marginLeft="@dimen/inter_item_margin_left"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
</RelativeLayout>
<include layout="@layout/horizontal_divider"
android:id="@+id/source_divider"
android:layout_marginTop="@dimen/intra_item_margin"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintTop_toBottomOf="@id/photo_source_edit_text_wrapper"/>
<LinearLayout
android:layout_marginStart="@dimen/inter_item_margin"
android:layout_marginEnd="@dimen/inter_item_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:gravity="center_horizontal"
android:weightSum="10"
android:orientation="horizontal">
<com.google.android.material.textfield.TextInputLayout
android:layout_marginTop="@dimen/intra_item_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tvFirstName"
android:hint="@string/first_name_hint"
android:fontFamily="@font/segoe_ui_light"
android:maxLength="20"
android:singleLine="true"
android:imeOptions="actionDone"
android:inputType="text"
android:gravity="start"
android:ellipsize="start"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_marginTop="@dimen/intra_item_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tvLastName"
android:hint="@string/last_name_hint"
android:fontFamily="@font/segoe_ui_light"
android:maxLength="20"
android:singleLine="true"
android:imeOptions="actionDone"
android:inputType="text"
android:gravity="start"
android:ellipsize="start"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_marginStart="@dimen/inter_item_margin"
android:layout_marginEnd="@dimen/inter_item_margin"
android:layout_marginTop="@dimen/intra_item_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
android:id="@+id/spGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_marginStart="@dimen/inter_item_margin"
android:layout_marginEnd="@dimen/inter_item_margin"
android:layout_marginTop="@dimen/intra_item_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tvZipCode"
android:hint="@string/confirm_password_hint"
android:fontFamily="@font/segoe_ui_light"
android:maxLength="20"
android:singleLine="true"
android:imeOptions="actionDone"
android:inputType="textPassword"
android:gravity="start"
android:ellipsize="start"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:layout_marginStart="@dimen/inter_item_margin"
android:layout_marginEnd="@dimen/inter_item_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:gravity="center_horizontal"
android:weightSum="25"
android:orientation="horizontal">
<TextView
android:id="@+id/tvBirthDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/inter_item_margin"
android:layout_marginEnd="@dimen/inter_item_margin"
android:layout_marginTop="@dimen/intra_item_margin"
android:text="DateTime"/>
</LinearLayout>
<include layout="@layout/horizontal_divider"
android:id="@+id/source_divider"
android:layout_marginTop="@dimen/intra_item_margin"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintTop_toBottomOf="@id/photo_source_edit_text_wrapper"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_marginStart="@dimen/inter_item_margin"
android:layout_marginEnd="@dimen/inter_item_margin"
android:layout_marginTop="@dimen/intra_item_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tvSecurityQuestion"
android:hint="@string/secret_question_hint"
android:fontFamily="@font/segoe_ui_light"
android:maxLength="20"
android:singleLine="true"
android:imeOptions="actionDone"
android:inputType="textPassword"
android:gravity="start"
android:ellipsize="start"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_marginStart="@dimen/inter_item_margin"
android:layout_marginEnd="@dimen/inter_item_margin"
android:layout_marginTop="@dimen/intra_item_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tvSecurityQuestionAnswer"
android:hint="@string/secret_question_answer_hint"
android:fontFamily="@font/segoe_ui_light"
android:maxLength="20"
android:singleLine="true"
android:imeOptions="actionDone"
android:inputType="textPassword"
android:gravity="start"
android:ellipsize="start"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<CheckBox
android:id="@+id/agree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/appbar_padding_top"
android:checked="true"
android:text="@string/checkbox_label_termscond" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:layout_margin="@dimen/inter_item_margin"
android:fontFamily="@font/segoe_ui_semi_bold"
android:text="@string/submit_btn"
app:backgroundTint="@color/bv_blue" />
<ProgressBar
android:id="@+id/pbRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</RelativeLayout>
Here is following error message produced in the logcat:
java.lang.IllegalStateException: ScrollView can host only one direct child
at android.widget.ScrollView.addView(ScrollView.java:273)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1466)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
Now I made sure that in the layout I only include one main layout with other nested layouts included in that main layout. So, based on that I don't think I should be getting that error. Any help or advice to fix this problem would be greatly appreciated!
Upvotes: 1
Views: 85
Reputation: 11110
There must be only one child view in ScrollView. You are getting this error because you are not using the full closing tag.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/loginFragmentContainer"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="true"
tools:context="com.bvmobileapps.bvmobileapps.LoginFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
....
</RelativeLayout>
</ScrollView>
Upvotes: 0
Reputation: 2817
You are missing a closing tag for your Scroll View in your xml files. Btw, Consider using nested scroll view instead of scroll view.(As mentioned in the documentation)
Also you should avoid using scroll view inside another scroll view.
Suggestion : Avoid nesting too much to enhance layout performance. Have a look : Performance and view hierarchies
Upvotes: 1