找到你要的答案

Q:Android ScrollView can host only one direct child

Q:Android ScrollView只能承载一个直接的孩子

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.

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

myactivity button listener

            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();

tracker fragment

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.

我从应该清除所有行内的TableView画廊onclick的听众,然后添加行/列在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 使用d
            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>

有谁知道什么是错的吗?我猜我添加表格行的滚动视图而不是TableView,然而,我不知道我在做我的代码错误。

answer1: 回答1:

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>

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

example

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

Hope this help.

事实上,你不能拥有像:

<?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只能承载一个直接的孩子

例子

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

希望这个帮助。

answer2: 回答2:

I know this question is quite old but I ran into the same problem and it end up being that you cant have a ScrollView as you base view for the layout.

Just wrap the Scrollview in your tracker.xml layout with a RelativeLayout and you should be good to go

我知道这个问题很老但我遇到同样的问题,最终,你不能有一个ScrollView你基地的视图布局。

把你的tracker.xml ScrollView布局一个RelativeLayout,你应该很好的去

answer3: 回答3:

Um... don't put multiple views inside of a scroll view. Put a single view inside of it that is the kind of layout you want for your multiple views -- TableLayout, FrameLayout, etc.

嗯...不要在滚动视图中放置多个视图。把一个视图里面,是布局的你想为你的多个视图,TableLayout,FrameLayout的那种,等。

answer4: 回答4:

Another possibility is that the closing element is not matching. Ie.

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

The error is in the last line.

另一种可能性是关闭元素不匹配。IE浏览器。

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

错误在最后一行。

answer5: 回答5:

You should add LinearLayout into ScrollView, it will working.

你应该添加元素到ScrollView,它将工作。

answer6: 回答6:

edit (2nd answer):

I've been playing with the code and I'm still not sure WHY this happens, definitely there's something wrong on the fragment manager when it swapping fragments (replace(resId, fragment))

In my fragment onCreateView was this code:

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

    return BuildView(view);

post_fragment is an empty canvas with 1 ScrollView, 1 LinearLayout inside it, and some other ViewGroups inside it. This is to load an empty canvas on the 1st load (item is null) and on the 2nd time, through a user input, it would get the actual data to show.

for some reason beyond my knowledge, as seen in the logcat, FragmentManagerImpl.moveToState was calling ScrollView.AddChild causing the error.

Now I changed my fragments onCreateView to:

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

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

so that the 'empty' canvas is a FrameLayout with nothing on it, and now everything is working happily. I'm sure this sounds like some odd error on the actual fragments framework, but it's a work around it.

1st answer:

Raptrex, as far as I can see. Yes, that table layout is the only child your scrowview have. I am having the exactly the same problem, and very frustrated trying to solve it.

From the LogCat/Debug, it seems that there's something in the Fragments manager that is calling and .AddView(view) in my ScrollView (check the log) it depends on how I change my XML layout I get this error or not, which let's say sounds pretty absurd!

Any ideas of how?why?what? I'm listening...

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)

edit (2nd answer):

我一直在玩的代码,我还是不知道为什么会发生这种情况,肯定有在片段的经理当交换片段错事(取代(渣油、片段))

在我oncreateview这个代码片段:

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

    return BuildView(view);

是一个有1 post_fragment ScrollView空白的画布,1元素在里面,和一些其他的viewgroup里面。这是加载一个空画布上的第一个负载(项目是空的),并在第二次,通过用户输入,它会得到实际数据显示。

因为某些原因,超出了我的知识,在LogCat中看到,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);

so that the 'empty' canvas is a FrameLayout with nothing on it, and now everything is working happily. I'm sure this sounds like some odd error on the actual fragments framework, but it's a work around it.

1st answer:

raptrex,尽我所能看到的。是的,那是唯一的孩子,你的表格布局scrowview有。我有完全相同的问题,非常沮丧试图解决它。

从logcat /调试,似乎在碎片是经理打来的,有什么东西。AddView(看)我在ScrollView(检查日志)这取决于我如何改变我的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)
answer7: 回答7:

Try to use a <FrameLayout> instead of the <fragment> in your main.xml

So instead of

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

use

<FrameLayout
    android:id="@+id/tracker1"
    android:layout_height="500dp"
    android:layout_width="300dp"
    android:layout_weight="1"
/>

It solved a similar problem with the ScrollView, where I was replacing the fragment from the activity with:

getSupportFragmentManager().beginTransaction()
    .replace(R.id.details_container, new DetailsFragment(), DETAILFRAGMENT_TAG)
    .commit();

尝试使用<;FrameLayout >;取而代之的是<;片段>;你在main.xml

所以不是

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

使用

<FrameLayout
    android:id="@+id/tracker1"
    android:layout_height="500dp"
    android:layout_width="300dp"
    android:layout_weight="1"
/>

它解决了ScrollView类似的问题,而我是替换片段的活性:

getSupportFragmentManager().beginTransaction()
    .replace(R.id.details_container, new DetailsFragment(), DETAILFRAGMENT_TAG)
    .commit();
android  android-layout  scrollview  android-fragments