Android ScrollView只能容纳一个直子 [英] Android ScrollView can host only one direct child

查看:59
本文介绍了Android ScrollView只能容纳一个直子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自库的onclick侦听器,该侦听器应清除表视图内的所有行,然后将行/行添加到滚动视图内的表视图中.每次单击按钮时,片段都应更改.

I have a onclick listener from the gallery that should clear all the rows inside the tableview, then add row/rows to a tableview inside a scrollview. The fragment should change on every button click.

但是我得到了:java.lang.IllegalStateException:ScrollView只能托管一个直接子代.

However I am getting: java.lang.IllegalStateException: ScrollView can host only one direct child.

我的活动按钮监听器

            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;
    }
}

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>

tracker.xml

<?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>

有人知道怎么了吗?我猜想我要向滚动视图中添加一个表行,而不是向表视图中添加,但是,我看不到我的代码在哪里做错了.

Does anyone know whats wrong? I am guessing I am adding a table row to the scrollview and not the tableview, however, I dont see where I am doing that wrong in my code.

推荐答案

实际上,您不能拥有以下内容:

In fact, you can't have things like :

<?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>

但是,如果表外还有一个额外的元素,则它会崩溃,并显示java.lang.IllegalStateException: ScrollView can host only one direct child

but if you have an extra element outside the table, it crash with an java.lang.IllegalStateException: ScrollView can host only one direct child

示例

<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"
>
<ImageView
android:id="@+id/logoImmoweb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logoimmoweb"
android:layout_gravity="top"
/>
<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"
>
...
</TableLayout>
</ScrollView>

希望获得帮助.

这篇关于Android ScrollView只能容纳一个直子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆