4

J'ai la liste des fichiers attachés et je veux que mon LinearLayout soit horizontalement défilable. J'ajoute seulement un enfant LinearLayout à mon HorizontalScrollView, en plus je reçois IllegalStateException. Mon xml:"IllegalStateException: scrollview ne peut héberger qu'un seul enfant direct" dans un LinearLayout ajoutant

<HorizontalScrollView 
android:id="@+id/scrollMessageFiles" 
android:layout_width="fill_parent" 
android:layout_height="65dp" 
android:layout_below="@+id/editMessage" 
android:orientation="horizontal" 
android:weightSum="1.0" > 
     <LinearLayout 
    android:id="@+id/panelMessageFiles" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:background="#FFFFFF" 
    > 

     </LinearLayout> 

     </HorizontalScrollView> 

et que vous souhaitez ajouter la liste des fichiers au LinearLayout dans le ScrollView comme ceci:

public void addFiles() 
{ 



    HorizontalScrollView scroll = (HorizontalScrollView) findViewById(R.id.scrollMessageFiles); 
    LinearLayout layout = (LinearLayout) findViewById(R.id.panelMessageFiles); 
    if(!FileManagerActivity.getFinalAttachFiles().isEmpty()) 
    { 
      for (File file: FileManagerActivity.getFinalAttachFiles()) 
       { 

        View line = new View(this); 

        line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT)); 
        line.setBackgroundColor(0xAA345556); 
        informationView = new TextView(this); 
        informationView.setTextColor(Color.BLACK); 
        informationView.setTextSize(12); 
        informationView.setCompoundDrawablesWithIntrinsicBounds(
          0, R.drawable.file_icon, 0, 0); 
        informationView.setText(file.getName().toString()); 
        layout.addView(informationView, 0); 
        layout.addView(line, 1); 


       } 
      scroll.addView(layout); 
     } 

} 

ajouter un seul LinearLayout à la HorizontalScrollView, en plus d'obtenir

 FATAL EXCEPTION: main 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.assignmentexpert/com.assignmentexpert.NewMessageActivity}: java.lang.IllegalStateException: HorizontalScrollView can host only one direct child 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
    at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:123) 
    at android.app.ActivityThread.main(ActivityThread.java:3687) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:507) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.IllegalStateException: HorizontalScrollView can host only one direct child 
    at android.widget.HorizontalScrollView.addView(HorizontalScrollView.java:223) 
    at com.assignmentexpert.NewMessageActivity.addFiles(NewMessageActivity.java:165) 
    at com.assignmentexpert.NewMessageActivity.onCreate(NewMessageActivity.java:90) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 

à la ligne

scroll.addView(layout); 
+2

Vous avez juste besoin de supprimer cette ligne: 'scroll.addView (layout);' Vous l'avez déjà déclaré en XML et vous essayez de l'ajouter à nouveau, c'est pourquoi vous obtenez l'exception de plusieurs enfants. – jnthnjns

Répondre

5

Vous avez juste besoin de supprimer cette ligne: scroll.addView(layout); Vous l'avez déjà déclaré en XML et essayez de l'ajouter à nouveau, c'est pourquoi vous obtenez l'exception de plusieurs enfants.

Essayez d'exécuter ceci:

public void addFiles() { 
    HorizontalScrollView scroll = (HorizontalScrollView) findViewById(R.id.scrollMessageFiles); 
    LinearLayout layout = (LinearLayout) findViewById(R.id.panelMessageFiles); 
    if(!FileManagerActivity.getFinalAttachFiles().isEmpty()) { 
     for (File file: FileManagerActivity.getFinalAttachFiles()) { 

      View line = new View(this); 

      line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT)); 
      line.setBackgroundColor(0xAA345556); 
      informationView = new TextView(this); 
      informationView.setTextColor(Color.BLACK); 
      informationView.setTextSize(12); 
      informationView.setCompoundDrawablesWithIntrinsicBounds(
        0, R.drawable.file_icon, 0, 0); 
      informationView.setText(file.getName().toString()); 
      layout.addView(informationView, 0); 
      layout.addView(line, 1); 

     } 
     // This line is telling the system to add your LinearLayout to the ScrollView when it is already there, declared in your xml layout file 
     //scroll.addView(layout); 
    } 

} 
2

Je suis désolé mais je ne l'ai pas pris la peine de lire votre code, l'erreur est très claire:

HorizontalScrollView can host only one direct child

ne mettez pas plus d'un enfant dans une vue de défilement horizontal! mettre un peu de vue de groupe là (mise en page linéaire, mise en page relative, ce que vous voulez) et sur ce groupe vous mettez les enfants.

3

Je résolus simplement enlever scroll.addView(layout);

+0

S'il vous plaît marquer une réponse comme acceptée, si vous avez résolu :) – jnthnjns

2

Retirez l'ancien enfant, avant d'essayer d'ajouter de nouvelles. Essayez:

scroll.removeAllViews(); 

avant

scroll.addView(layout); 
+1

Juste curieux de savoir pourquoi vous supprimer le 'LinearLayout', ne semble pas nécessaire, il est configuré correctement dans son xml et il y ajoute des enfants. Pourquoi ne pas simplement supprimer 'scroll.addView (layout);'? – jnthnjns

+1

Vous avez raison fonctionne aussi et est plus facile.:-) – slezadav

+0

+1 pour fournir une solution qui fonctionne :-) – jnthnjns

2

Un ScrollView ne peut avoir qu'un seul enfant, il n'a pas de sens d'ajouter d'autres enfants directement. Disons que votre ScrollView a une LinearLayout à l'intérieur de celui-ci, vous pouvez ajouter plus de vues à la LinearLayout:

+0

une explication soignée et douce –

3
<HorizontalScrollView 
     android:id="@+id/btnholder" 
     android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:layout_marginTop="131dp" 
     android:scrollbars="horizontal" > 

    <LinearLayout 
     android:id="@+id/holderscroll" 
     android:layout_width="fill_parent" 
     android:layout_height="55dp" 
     android:layout_marginTop="2dp" > 

    </LinearLayout> 
    </HorizontalScrollView> 

subcategoryscrollV = (HorizontalScrollView) findViewById (R.id.btnholder);

addbtnlinearlay = (LinearLayout) findViewById (R.id.holderscroll);

Et ajoutez votre bouton sur la mise en page:

addbtnlinearlay.addView (BTN [i]);

Questions connexes