Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ScrollView只能承载一个直接子级_Android_Android Layout_Scrollview_Android Fragments - Fatal编程技术网

Android ScrollView只能承载一个直接子级

Android ScrollView只能承载一个直接子级,android,android-layout,scrollview,android-fragments,Android,Android Layout,Scrollview,Android Fragments,我有一个来自gallery的onclick侦听器,它应该清除tableview中的所有行,然后将行添加到scrollview中的tableview中。每次点击按钮时,片段都会发生变化 但是我得到了:java.lang.IllegalStateException:ScrollView只能承载一个直接子级 myactivity按钮侦听器 TrackerFragment tf = (TrackerFragment) getFragmentManager().findFragme

我有一个来自gallery的onclick侦听器,它应该清除tableview中的所有行,然后将行添加到scrollview中的tableview中。每次点击按钮时,片段都会发生变化

但是我得到了:java.lang.IllegalStateException:ScrollView只能承载一个直接子级

myactivity按钮侦听器

            TrackerFragment tf = (TrackerFragment) getFragmentManager().findFragmentById(R.id.tracker1);
            tf = TrackerFragment.newInstance(listOfList.get(id).get(position));

            fragmentTransaction.add(R.id.tracker1, tf);
            fragmentTransaction.commit();
            t1 = true; //being used
            getFragmentManager().executePendingTransactions();
跟踪器碎片

public class TrackerFragment extends Fragment
{
    private Dish dish;

    public static TrackerFragment newInstance(Serializable dish) 
    {
        TrackerFragment tf = new TrackerFragment();

        Bundle args = new Bundle();
        args.putSerializable("dish", dish);
        tf.setArguments(args);

        return tf;
    }

    public static TrackerFragment newInstance(Bundle bundle)
    {
        Dish dish = (Dish) bundle.getSerializable("dish");
        return newInstance(dish);
    }

    @Override
    public void onCreate(Bundle myBundle)
    {
        super.onCreate(myBundle);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 
    {
        View v = inflater.inflate(R.layout.tracker, container, false);

        TableLayout tableLayout = (TableLayout) v.findViewById(R.id.tracker_layout);
        tableLayout.removeAllViews();

        if (dish != null)
        {   
            //display others

            //display subsystem stuff
            for (int i = 0; i < dish.getSubsystems().size(); i++)
            {
                TableRow tableRow = new TableRow(v.getContext());
                tableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

                TextView tv = new TextView(v.getContext());
                tv.setText(dish.getSubsystems().get(i).getConnFuncAddr());

                tableRow.addView(tv);

                tableLayout.addView(tableRow);
            }
        }
        else
        {
            TableRow tableRow = new TableRow(v.getContext());
            tableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

            TextView tv = new TextView(v.getContext());
            tv.setText("Click on image to view more data");

            tableRow.addView(tv);

            tableLayout.addView(tableRow);
        }

        return v;
    }
}
公共类TrackerFragment扩展片段
{
私家菜;
公共静态TrackerFragment newInstance(可序列化的碟形盘)
{
TrackerFragment tf=新TrackerFragment();
Bundle args=新Bundle();
args.putSerializable(“dish”,dish);
tf.setArguments(args);
返回tf;
}
公共静态TrackerFragment newInstance(捆绑包)
{
Dish Dish=(Dish)bundle.getSerializable(“Dish”);
返回新实例(dish);
}
@凌驾
创建公共void(Bundle myBundle)
{
super.onCreate(myBundle);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState)
{
视图v=充气机。充气(R.layout.tracker,容器,假);
TableLayout TableLayout=(TableLayout)v.findViewById(R.id.tracker\u布局);
tableLayout.removeallview();
if(dish!=null)
{   
//展示他人
//显示子系统材料
对于(int i=0;i
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Trackers -->
    <LinearLayout
        android:layout_height="fill_parent"
        android:layout_width="300dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <fragment 
            android:name="android.gallery.TrackerFragment"
            android:id="@+id/tracker1"
            android:layout_height="500dp"
            android:layout_width="300dp"
            android:layout_weight="1">
        </fragment>

    </LinearLayout>

    <!-- Gallery -->
    <LinearLayout
        android:layout_height="fill_parent"
        android:layout_width="600dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="245dp">

            <Gallery 
                android:id="@+id/galleryid0"
                android:layout_width="fill_parent" 
                android:layout_height="match_parent"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="245dp">

            <Gallery 
                android:id="@+id/galleryid1"
                android:layout_width="fill_parent" 
                android:layout_height="match_parent"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="245dp">

            <Gallery 
                android:id="@+id/galleryid2"
                android:layout_width="fill_parent" 
                android:layout_height="match_parent"/>

            </LinearLayout>

        </LinearLayout>

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

    <TableLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:id="@+id/tracker_layout">

    </TableLayout>

</ScrollView>

tracker.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Trackers -->
    <LinearLayout
        android:layout_height="fill_parent"
        android:layout_width="300dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <fragment 
            android:name="android.gallery.TrackerFragment"
            android:id="@+id/tracker1"
            android:layout_height="500dp"
            android:layout_width="300dp"
            android:layout_weight="1">
        </fragment>

    </LinearLayout>

    <!-- Gallery -->
    <LinearLayout
        android:layout_height="fill_parent"
        android:layout_width="600dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="245dp">

            <Gallery 
                android:id="@+id/galleryid0"
                android:layout_width="fill_parent" 
                android:layout_height="match_parent"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="245dp">

            <Gallery 
                android:id="@+id/galleryid1"
                android:layout_width="fill_parent" 
                android:layout_height="match_parent"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="245dp">

            <Gallery 
                android:id="@+id/galleryid2"
                android:layout_width="fill_parent" 
                android:layout_height="match_parent"/>

            </LinearLayout>

        </LinearLayout>

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

    <TableLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:id="@+id/tracker_layout">

    </TableLayout>

</ScrollView>


有人知道怎么了吗?我猜我是在向scrollview而不是tableview添加一个表行,但是,我看不出我在代码中犯了什么错误。

嗯。。。不要在滚动视图中放置多个视图。在其中放置一个视图,该视图是您希望用于多个视图的布局类型—TableLayout、FrameLayout等。

edit(第二个答案): 我一直在玩代码,我仍然不知道为什么会发生这种情况,当片段管理器交换片段(replace(resId,fragment))时,它肯定出了问题

在我的片段onCreateView中有以下代码:

    View view = inflater.inflate(R.layout.post_fragment, null, false);
    if (item == null)
        return view;

    return BuildView(view);
post_片段是一个空画布,其中包含1个ScrollView、1个LinearLayout和一些其他视图组。这是在第一次加载时加载一个空画布(项为空),第二次,通过用户输入,它将获得要显示的实际数据

由于一些我所不知道的原因,如日志中所示,FragmentManagerImpl.moveToState调用ScrollView.AddChild导致了错误

现在,我将片段onCreateView更改为:

    if (item == null)
        return inflater.inflate(R.layout.empty, null, false);

    View view = inflater.inflate(R.layout.post_fragment, null, false);
    return BuildView(view);
所以“空”画布是一个框架布局,上面什么都没有,现在一切都很顺利。 我确信这听起来像是实际片段框架上的一些奇怪错误,但这是一个解决方法

第一个答复: 就我所能看到的而言,是Raptrex。是的,该表布局是scrowview的唯一子级。我遇到了完全相同的问题,并且很沮丧地试图解决它

从LogCat/Debug来看,碎片管理器中似乎有什么东西在调用,而我的ScrollView(检查日志)中的.AddView(视图)则取决于我如何更改XML布局,我是否会遇到这个错误,这听起来很荒谬

知道怎么做吗?为什么?什么?我在听

FATAL EXCEPTION: main
java.lang.IllegalStateException: ScrollView can host only one direct child
at android.widget.ScrollView.addView(ScrollView.java:219)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:787)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:977)
at android.app.BackStackRecord.run(BackStackRecord.java:638)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1309)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:398)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4123)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)

事实上,你不能有这样的东西:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="@+id/widget27"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffffff"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TableLayout
android:id="@+id/layoutOption"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffffff"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<ImageView
android:id="@+id/logoImmoweb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logoimmoweb"
android:layout_gravity="top"
/>
 ...
</TableLayout>
</ScrollView>

希望对您有所帮助。

我知道这个问题很老了,但我遇到了同样的问题,结果是您不能将
滚动视图作为布局的基本视图


只需在tracker.xml布局中使用
RelativeLayout
将滚动视图包装起来,您就可以开始了

另一种可能是结束元素不匹配。即

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content">
    <LinearLayout ...>
        ...
    </LinearLayout>
</LinearLayout>

...

错误在最后一行

您应该在ScrollView中添加LinearLayout,它会起作用。

尝试使用
而不是main.xml中的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Trackers -->
    <LinearLayout
        android:layout_height="fill_parent"
        android:layout_width="300dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <fragment 
            android:name="android.gallery.TrackerFragment"
            android:id="@+id/tracker1"
            android:layout_height="500dp"
            android:layout_width="300dp"
            android:layout_weight="1">
        </fragment>

    </LinearLayout>

    <!-- Gallery -->
    <LinearLayout
        android:layout_height="fill_parent"
        android:layout_width="600dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="245dp">

            <Gallery 
                android:id="@+id/galleryid0"
                android:layout_width="fill_parent" 
                android:layout_height="match_parent"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="245dp">

            <Gallery 
                android:id="@+id/galleryid1"
                android:layout_width="fill_parent" 
                android:layout_height="match_parent"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="245dp">

            <Gallery 
                android:id="@+id/galleryid2"
                android:layout_width="fill_parent" 
                android:layout_height="match_parent"/>

            </LinearLayout>

        </LinearLayout>

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

    <TableLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:id="@+id/tracker_layout">

    </TableLayout>

</ScrollView>
所以不是

<fragment 
    android:name="android.gallery.TrackerFragment"
    android:id="@+id/tracker1"
    android:layout_height="500dp"
    android:layout_width="300dp"
    android:layout_weight="1">
</fragment>

上周我遇到了这个问题,然后找到了这个问题的最佳答案


我的scrollview中的唯一子视图不是tablelayout吗?如果发生异常,您将在scroll视图中获得多个视图。为什么?他在下面添加了一个例外。显然,他是在向滚动视图添加片段的视图层次结构。那么“项目”是什么?在onCreateView()解决方案中初始化的某种变量: