gpt4 book ai didi

java - "ScrollView can only host 1 child"在 2 个 fragment 中,只有 1 个 child ?

转载 作者:行者123 更新时间:2023-12-02 08:50:52 26 4
gpt4 key购买 nike

我有 2 个 fragment ,每个 fragment 都有一个 ScrollView,里面有 1 个 ConstraintLayout。 ContraintLayout 内部包含多个子级,但 ScrollViews 只有 1 个子级。当我转到 fragment 1 时,我没有收到任何错误。我转到 fragment 2,一切都很好。然而,当我返回 fragment 1 时,出现以下错误:

2020-03-20 22:56:20.491 7887-7887/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.starenkysoftware.vanshapp, PID: 7887
java.lang.IllegalStateException: ScrollView can host only one direct child
at android.widget.ScrollView.addView(ScrollView.java:266)
at com.google.android.material.snackbar.BaseTransientBottomBar.showView(BaseTransientBottomBar.java:719)
at com.google.android.material.snackbar.BaseTransientBottomBar$1.handleMessage(BaseTransientBottomBar.java:243)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6898)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

我不明白这是怎么可能的,因为我在切换时没有在布局中添加任何其他内容

编辑:添加代码

fragment 1:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/recipient_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Beneficiary"
android:textStyle="bold"
android:textSize="15sp"
android:textColor="@color/colorPrimary"
android:layout_marginTop="20dp"
android:layout_marginLeft="18dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>

<View
android:id="@+id/recipient_splitter"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="3dp"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
android:background="@color/colorPrimary"
app:layout_constraintTop_toBottomOf="@id/recipient_header"/>

<!-- Recipient Name Fields -->

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/recipient_first_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
app:layout_constraintHorizontal_chainStyle="spread"
android:layout_marginRight="10dp"
app:layout_constraintTop_toBottomOf="@id/recipient_splitter"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/recipient_middle_name"
android:hint="First Name">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/first_name_field"
android:inputType="textPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/recipient_middle_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintTop_toBottomOf="@id/recipient_splitter"
app:layout_constraintLeft_toRightOf="@id/recipient_first_name"
app:layout_constraintRight_toLeftOf="@id/recipient_last_name"
android:hint="Middle Name">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/middle_name_field"
android:inputType="textPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/recipient_last_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/recipient_first_name"
app:layout_constraintBottom_toBottomOf="@+id/recipient_first_name"
app:layout_constraintLeft_toRightOf="@id/recipient_middle_name"
android:hint="Last Name">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/last_name_field"
android:inputType="textPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

<!-- Recipient Location Field -->

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/recipient_state"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
app:layout_constraintHorizontal_chainStyle="spread"
android:layout_marginRight="10dp"
app:layout_constraintTop_toBottomOf="@id/recipient_first_name"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/recipient_city"
android:hint="Province">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/state_field"
android:inputType="textPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/recipient_city"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/recipient_state"
app:layout_constraintBottom_toBottomOf="@+id/recipient_state"
app:layout_constraintLeft_toRightOf="@id/recipient_state"
android:hint="City">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/city_field"
android:inputType="textPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

<!-- Phone Number -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/phone_number_layout"
app:prefixText=""
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
app:layout_constraintTop_toBottomOf="@id/recipient_state"
android:hint="Phone Number">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/phone_number_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"/>

</com.google.android.material.textfield.TextInputLayout>

<!-- Transger Info Section -->
<TextView
android:id="@+id/transfer_info_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Transfer Info"
android:textStyle="bold"
android:textSize="15sp"
android:textColor="@color/colorPrimary"
android:layout_marginTop="25dp"
android:layout_marginLeft="18dp"
app:layout_constraintTop_toBottomOf="@id/phone_number_layout"
app:layout_constraintLeft_toLeftOf="parent"/>

<View
android:id="@+id/transfer_info_splitter"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="3dp"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
android:background="@color/colorPrimary"
app:layout_constraintTop_toBottomOf="@id/transfer_info_header"/>

<!-- SOF (Source of Fund -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/sof_layout"
app:prefixText=""
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
app:layout_constraintTop_toBottomOf="@id/transfer_info_splitter"
android:hint="Source of Fund">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/sof_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>

<!-- Type of Transfer Dropdown -->
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:id="@+id/type_of_transfer_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="Type"
app:layout_constraintTop_toBottomOf="@id/sof_layout">

<AutoCompleteTextView
android:id="@+id/type_of_transfer_dropdown"
android:editable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/btb_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/type_of_transfer_layout">

<!-- Bank Name -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/bank_name_layout"
app:prefixText=""
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
app:layout_constraintTop_toTopOf="parent"
android:hint="Bank Name">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/bank_name_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>

<!-- Phone Number -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/bank_account_number_layout"
app:prefixText=""
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
app:layout_constraintTop_toBottomOf="@id/bank_name_layout"
android:hint="Bank Account Number">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/bank_account_number_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

<!-- Currency Selector -->
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:id="@+id/currency_selector_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
app:layout_constraintBottom_toBottomOf="@id/amount_layout"
app:layout_constraintTop_toTopOf="@id/amount_layout"
app:layout_constraintLeft_toLeftOf="parent">

<AutoCompleteTextView
android:id="@+id/currency_dropdown"
android:editable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

<!-- Amount Field -->


<com.google.android.material.textfield.TextInputLayout
android:id="@+id/amount_layout"
app:prefixText=""
app:suffixText="CAD"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="20dp"
app:layout_constraintTop_toBottomOf="@id/btb_info"
app:layout_constraintLeft_toRightOf="@id/currency_selector_layout"
android:hint="Amount">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/amount_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />

</com.google.android.material.textfield.TextInputLayout>

<TextView
android:id="@+id/currency_rate_small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text=""
android:textSize="13sp"
app:layout_constraintBottom_toTopOf="@+id/photo_checkbox"
app:layout_constraintLeft_toLeftOf="@+id/amount_layout"
app:layout_constraintTop_toBottomOf="@id/amount_layout" />

<com.google.android.material.button.MaterialButton
android:id="@+id/submit_button"
style="@style/Widget.MaterialComponents.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:text="Send"
app:layout_constraintTop_toBottomOf="@+id/amount_layout"
app:layout_constraintRight_toRightOf="parent"/>

<com.google.android.material.button.MaterialButton
android:id="@+id/photo_button"
style="@style/Widget.MaterialComponents.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:text="Photo Verification"
app:layout_constraintTop_toBottomOf="@+id/amount_layout"
app:layout_constraintLeft_toLeftOf="parent"/>

<CheckBox
android:id="@+id/photo_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:clickable="false"
app:layout_constraintBottom_toBottomOf="@id/photo_button"
app:layout_constraintLeft_toRightOf="@id/photo_button"
app:layout_constraintTop_toTopOf="@+id/photo_button"/>

<View
android:layout_width="match_parent"
android:layout_height="30dp"
app:layout_constraintTop_toBottomOf="@id/submit_button"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

fragment 2:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/previous_transfer_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Previous 5 Transfers"
android:gravity="center"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.card.MaterialCardView
android:id="@+id/materialCardView1"
style="@style/Widget.MaterialComponents.CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/mtrl_card_spacing"
android:layout_marginRight="@dimen/mtrl_card_spacing"
android:layout_marginTop="15dp"
android:minHeight="100dp"
app:cardBackgroundColor="#ffffff"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/previous_transfer_title">

<TextView
android:id="@+id/transfer_card_1_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:text="Exchange Rate"
android:textColor="#878787"
android:textSize="20dp" />

<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_gravity="bottom"
android:background="@color/colorPrimary" />

</com.google.android.material.card.MaterialCardView>

<com.google.android.material.card.MaterialCardView
android:id="@+id/materialCardView2"
style="@style/Widget.MaterialComponents.CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/mtrl_card_spacing"
android:layout_marginRight="@dimen/mtrl_card_spacing"
android:layout_marginTop="20dp"
android:minHeight="100dp"
app:cardBackgroundColor="#ffffff"
app:layout_constraintTop_toBottomOf="@+id/materialCardView1">

<TextView
android:id="@+id/transfer_card_2_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:text="Exchange Rate"
android:textColor="#878787"
android:textSize="20dp" />

<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_gravity="bottom"
android:background="@color/colorPrimary" />

</com.google.android.material.card.MaterialCardView>

<com.google.android.material.card.MaterialCardView
android:id="@+id/materialCardView3"
style="@style/Widget.MaterialComponents.CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/mtrl_card_spacing"
android:layout_marginRight="@dimen/mtrl_card_spacing"
android:layout_marginTop="20dp"
android:minHeight="100dp"
app:cardBackgroundColor="#ffffff"
app:layout_constraintTop_toBottomOf="@+id/materialCardView2">

<TextView
android:id="@+id/transfer_card_3_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:text="Exchange Rate"
android:textColor="#878787"
android:textSize="20dp" />

<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_gravity="bottom"
android:background="@color/colorPrimary" />

</com.google.android.material.card.MaterialCardView>

<com.google.android.material.card.MaterialCardView
android:id="@+id/materialCardView4"
style="@style/Widget.MaterialComponents.CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/mtrl_card_spacing"
android:layout_marginRight="@dimen/mtrl_card_spacing"
android:layout_marginTop="20dp"
android:minHeight="100dp"
app:cardBackgroundColor="#ffffff"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialCardView3">

<TextView
android:id="@+id/transfer_card_4_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:text="Exchange Rate"
android:textColor="#878787"
android:textSize="20dp" />

<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_gravity="bottom"
android:background="@color/colorPrimary" />

</com.google.android.material.card.MaterialCardView>

<com.google.android.material.card.MaterialCardView
android:id="@+id/materialCardView5"
style="@style/Widget.MaterialComponents.CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/mtrl_card_spacing"
android:layout_marginRight="@dimen/mtrl_card_spacing"
android:layout_marginTop="20dp"
android:layout_marginBottom="15dp"
android:minHeight="100dp"
app:cardBackgroundColor="#ffffff"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialCardView4"
app:layout_constraintBottom_toBottomOf="parent">

<TextView
android:id="@+id/transfer_card_5_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:text="Exchange Rate"
android:textColor="#878787"
android:textSize="20dp" />

<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_gravity="bottom"
android:background="@color/colorPrimary" />

</com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>


</androidx.constraintlayout.widget.ConstraintLayout>

最佳答案

我有附在元素上的 snackbar 。它们在某些情况下工作得很好,但由于某种原因并不总是如此。我认为这可能与退出时 fragment 的创建和销毁方式有关。

我用 toast 代替了 snackbar ,它解决了问题,尽管外观变得更糟。

关于java - "ScrollView can only host 1 child"在 2 个 fragment 中,只有 1 个 child ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60784089/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com