Android ScrollView can contain only one direct child

I have an onclick listener from the gallery, which should clear all the rows inside the tableview and then add row / rows to the tableview inside the scrollview. The fragment should change each time the button is pressed.

However, I get: java.lang.IllegalStateException: ScrollView can contain only one direct child element.

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 what happened? I am assuming that I am adding a table row to scrollview and not to tableview, however I cannot see where I am doing it wrong in my code.

+7
source share
7 answers

In fact, you cannot 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 is broken using 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> 

I hope for this help.

+5
source

I know this question is quite old, but I ran into the same problem, and ultimately you don't have ScrollView as the base view for the layout.

Just circle Scrollview in tracker.xml layout using RelativeLayout and you should be good to go

+3
source

You have to add LinearLayout to ScrollView, it will work.

+3
source

Another possibility is that the closing element does not match. I.e.

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

Error in the last line.

+2
source

Um ... don't put multiple views inside the scroll. Put one view in it, which is the layout you want to use for several views - TableLayout, FrameLayout, etc.

+1
source

edit (2nd answer):

I played with the code and I'm still not sure WHY this is happening, there is definitely something wrong with the fragment manager when it exchanges fragments (replace (resId, fragment))

In my onCreateView snippet there 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. It needs to load an empty canvas on the 1st load (the element is null), and in the second - through user input, it will receive the actual data for display.

for some reason, as far as I know, as shown in logcat, FragmentManagerImpl.moveToState caused a ScrollView.AddChild error.

Now I changed my onCreateView snippets 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 the "empty" canvas is FrameLayout, on which there is nothing, and now everything works happily. I'm sure this sounds like an odd mistake in the real frame structure, but it works around it.

1st answer:

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

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

Any ideas on how? why? I'm listening to...

 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) 
0
source

Try using <FrameLayout> instead of <fragment> in main.xml

So instead

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

using

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

He solved a similar problem with ScrollView, where I replaced the fragment from the action:

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

All Articles