“ScrollView 只能承载 1 个孩子”在 2 个片段中只有 1 个孩子?
Posted
技术标签:
【中文标题】“ScrollView 只能承载 1 个孩子”在 2 个片段中只有 1 个孩子?【英文标题】:"ScrollView can only host 1 child" in 2 fragments with only 1 child? 【发布时间】:2020-07-02 04:14:22 【问题描述】:我有 2 个片段,每个片段都有一个 ScrollView,内部有 1 个 ConstraintLayout。 ContraintLayout 内部包含多个孩子,但 ScrollViews 只有 1 个孩子。当我转到片段 1 时,我没有收到任何错误。我去片段2,一切都很好。但是,当我返回片段 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)
我不明白这是怎么可能的,因为我在切换时没有在布局中添加任何其他内容
编辑:添加代码
片段 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_
android:layout_>
<ScrollView
android:layout_
android:layout_>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_
android:layout_>
<TextView
android:id="@+id/recipient_header"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/recipient_middle_name"
android:layout_
android:layout_
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_
android:layout_/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/recipient_last_name"
android:layout_
android:layout_
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_
android:layout_/>
</com.google.android.material.textfield.TextInputLayout>
<!-- Recipient Location Field -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/recipient_state"
android:layout_
android:layout_
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_
android:layout_/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/recipient_city"
android:layout_
android:layout_
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_
android:layout_/>
</com.google.android.material.textfield.TextInputLayout>
<!-- Phone Number -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/phone_number_layout"
app:prefixText=""
android:layout_
android:layout_
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_
android:layout_
android:inputType="phone"/>
</com.google.android.material.textfield.TextInputLayout>
<!-- Transger Info Section -->
<TextView
android:id="@+id/transfer_info_header"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_/>
</com.google.android.material.textfield.TextInputLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/btb_info"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_/>
</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_
android:layout_
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_
android:layout_
android:inputType="numberDecimal" />
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:id="@+id/currency_rate_small"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
app:layout_constraintTop_toBottomOf="@id/submit_button"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
片段 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_
android:layout_>
<ScrollView
android:layout_
android:layout_>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_
android:layout_>
<TextView
android:id="@+id/previous_transfer_title"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:text="Exchange Rate"
android:textColor="#878787"
android:textSize="20dp" />
<View
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:text="Exchange Rate"
android:textColor="#878787"
android:textSize="20dp" />
<View
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:text="Exchange Rate"
android:textColor="#878787"
android:textSize="20dp" />
<View
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:text="Exchange Rate"
android:textColor="#878787"
android:textSize="20dp" />
<View
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:text="Exchange Rate"
android:textColor="#878787"
android:textSize="20dp" />
<View
android:layout_
android:layout_
android:layout_gravity="bottom"
android:background="@color/colorPrimary" />
</com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】:
@AndreyStarenky,在片段之间切换时,您是否向 ScrollView 添加了任何新视图? @PhanVanLinh 不,我 99% 确定我没有添加任何内容。我可能错过了一些非常小的东西,如果我找到任何东西,我会告诉你 【参考方案1】:我有附在物品上的小吃店。它们在某些情况下工作得很好,但由于某种原因并非总是如此。我认为这可能与 Fragments 在退出时的创建和销毁方式有关。
我用吐司代替了我的小吃店,它解决了这个问题,尽管外观变得更糟了。
【讨论】:
以上是关于“ScrollView 只能承载 1 个孩子”在 2 个片段中只有 1 个孩子?的主要内容,如果未能解决你的问题,请参考以下文章
NOIP 2015 & SDOI 2016 Round1 & CTSC 2016 & SDOI2016 Round2游记