即使在布局XML代码中仅使用一个主布局,我仍然得到了“ ScrollView只能容纳一个直接子代”的信息。基本上,我想做的是从LoginFragment调用/启动RegisterFragment。我正在尝试使用以下代码来做到这一点:
@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;
}
}
以下是这两个XML文件的代码,它们可能会导致错误:
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>
以下是在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)
现在,我确保在布局中只包含一个主布局,而其他嵌套布局包含在该主布局中。因此,基于此,我认为我应该不会收到该错误。任何帮助或建议解决此问题将不胜感激!
答案 0 :(得分:1)
您的 xml 文件中缺少滚动视图的结束标记。 顺便说一句,考虑使用嵌套滚动视图而不是滚动视图。(如文档中所述)
你也应该避免在另一个滚动视图中使用滚动视图。
建议:避免嵌套过多以提高布局性能。 看看:Performance and view hierarchies
答案 1 :(得分:0)
ScrollView 中必须只有一个子视图。您收到此错误是因为您没有使用完整的结束标记。
<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>