android-fragments 为什么会出现“ScrollView只能承载一个直接子级”错误?

58wvjzkj  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(135)

我收到了“ScrollView只能托管一个直接子节点”错误。我意识到这通常(总是?)是由ScrollView有多个子节点引起的-堆栈上的多个答案hereherehereherehere表示要将子节点 Package 在LinearLayout或类似布局中。
但是我确信我的ScrollView只有一个子对象。请看下面的代码:

布局\菜单.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_medium"
    android:orientation="vertical">

    <include
        android:id="@+id/menu_header"
        layout="@layout/header" />

    <ScrollView
        android:id="@+id/menu_wrapper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <LinearLayout
            android:id="@+id/menu_content"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">

            <!-- Various LinearLayouts displaying menu items -->
            
        </LinearLayout>
    </ScrollView>
</LinearLayout>

菜单.java

// When one of the menu items is clicked
FragmentTransaction transaction = getParentFragmentManager().beginTransaction();
transaction.replace(R.id.menu_wrapper, fragment);
transaction.addToBackStack(null);
transaction.commit();

布局\片段.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_light"
    android:orientation="vertical">

    <include
        android:id="@+id/list_header"
        layout="@layout/header" />

    <include
        android:id="@+id/list_sort"
        layout="@layout/sort" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/inspection_list_header"
        android:layout_alignParentBottom="true">

        <ListView
            android:id="@+id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@+id/list_loading"
            android:divider="@color/background_medium"
            android:dividerHeight="1dp"
            android:listSelector="@drawable/default_focused"
            android:textSize="18sp">

        </ListView>

        <ProgressBar
            android:id="@+id/list_loading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:indeterminate="true"
            android:indeterminateOnly="true" />
    </RelativeLayout>
</LinearLayout>

片段.java

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
  View rootView = inflater.inflate(R.layout.list, container, false);
  this.inspectionListView = rootView;
  return rootView;
}

下面是堆栈跟踪:

2022-04-25 13:12:59.725 14833-14833/com.*******.*******.mobile E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.*******.*******.mobile, PID: 14833
    java.lang.IllegalStateException: ScrollView can host only one direct child
        at android.widget.ScrollView.addView(ScrollView.java:494)
        at androidx.fragment.app.FragmentStateManager.addViewToContainer(FragmentStateManager.java:833)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:523)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
        at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7211)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)

我肯定我错过了一些非常简单的东西,但我不知道是什么。我继承了这段代码,不是一个Android开发人员,所以任何帮助都将不胜感激

tp5buhyn

tp5buhyn1#

menu_content给予高度和宽度匹配父项

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background_medium"
        android:orientation="vertical">
    
        <include
            android:id="@+id/menu_header"
            layout="@layout/header" />
    

        <!-- This view will be used as fragment container -->

        <LinearLayout
                android:id="@+id/menu_content"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"/>

    </LinearLayout>

相关问题