【问题标题】:"IllegalStateException : scrollview can host only one direct child" in one LinearLayout adding“IllegalStateException:滚动视图只能承载一个直接子级”在一个 LinearLayout 中添加
【发布时间】:2012-10-26 15:14:17
【问题描述】:

我有一个附加文件列表,并且想让我的 LinearLayout 可以水平滚动。我只向我的 Horizo​​ntalScrollView 添加了一个子 LinearLayout,除了我得到 IllegalStateException。我的xml:

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

想要添加文件列表 像这样到 ScrollView 中的 LinearLayout:

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

}

我只给Horizo​​ntalScrollView添加了一个LinearLayout,除了get

        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)

在线上

 scroll.addView(layout);

【问题讨论】:

  • 你只需要删除这一行:scroll.addView(layout); 你已经在 xml 中声明了这个并且正在尝试再次添加它,这就是你得到多个子异常的原因。

标签: android textview android-linearlayout horizontalscrollview illegalstateexception


【解决方案1】:

您只需要删除此行:scroll.addView(layout); 您已经在 xml 中声明了此行并尝试再次添加它,这就是您收到多子异常的原因。

尝试运行这个:

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

    我解决了简单地删除scroll.addView(layout);

    【讨论】:

      【解决方案3】:

      删除旧的孩子,然后再尝试添加新的。试试:

      scroll.removeAllViews();
      

      之前

      scroll.addView(layout);
      

      【讨论】:

      • 只是好奇你为什么要删除LinearLayout,似乎没有必要,它在他的xml中设置正确,他正在向其中添加孩子。为什么不直接删除scroll.addView(layout);
      【解决方案4】:
      <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 = (Horizo​​ntalScrollView)findViewById(R.id.btnholder);

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

      并在布局上添加您的按钮:

      addbtnlinearlay.addView(btn[i]);

      【讨论】:

        【解决方案5】:

        对不起,我什至懒得看你的代码,错误很清楚:

        HorizontalScrollView can host only one direct child

        不要在水平滚动视图中放置多个孩子! 在那里放一些组视图(线性布局,相对布局,任何你想要的),然后在那个组上放孩子。

        【讨论】:

          【解决方案6】:

          一个 ScrollView 只能有一个孩子,所以直接向它添加更多的孩子是没有意义的。假设您的 ScrollView 内部有一个 LinearLayout,那么您可以向 LinearLayout 添加更多视图:

          【讨论】:

          • 简洁甜美的解释
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多