2017-01-23 50 views
0

我有一個滾動視圖和一些線性佈局的佈局。並看看我的代碼,只有一個線性佈局,這是滾動視圖只有一個孩子Scrollview只有一個孩子,但例外

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/content_home" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.mng.activity.HomeActivity" 
    tools:showIn="@layout/app_bar_home"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:weightSum="2" 
     android:id="@+id/home_menu"> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:weightSum="2"> 

      <android.support.v7.widget.CardView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_weight="1" 
       android:layout_margin="10dp" 
       android:id="@+id/st"> 

       <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_margin="10dp"> 

        <ImageView 
         android:layout_width="match_parent" 
         android:layout_height="57dp" 
         app:srcCompat="@drawable/ic_reading" 
         android:id="@+id/image" /> 

        <TextView 
          android:text="@string/student" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:id="@+id/student" 
          android:layout_gravity="center_horizontal" 
          android:textColor="@android:color/black" 
         android:padding="5dp" 
          android:gravity="center" /> 
       </LinearLayout> 

      </android.support.v7.widget.CardView> 

      <android.support.v7.widget.CardView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_weight="1" 
       android:layout_margin="10dp" 
       android:id="@+id/pa"> 

       <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_margin="10dp"> 

        <ImageView 
         android:layout_width="match_parent" 
         android:layout_height="57dp" 
         app:srcCompat="@drawable/ic_parent" 
         android:id="@+id/imageVie2" /> 

        <TextView 
         android:text="@string/parent" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:id="@+id/parent" 
         android:layout_gravity="center_horizontal" 
         android:textColor="@android:color/black" 
         android:padding="5dp" 
         android:gravity="center" /> 
       </LinearLayout> 
      </android.support.v7.widget.CardView> 

     </LinearLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.39" 
      android:weightSum="2"> 

      <android.support.v7.widget.CardView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:layout_margin="10dp" 
       android:id="@+id/te"> 
       <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_margin="10dp"> 

        <ImageView 
         android:layout_width="match_parent" 
         android:layout_height="57dp" 
         app:srcCompat="@drawable/ic_teacher" 
         android:id="@+id/imageView2" /> 

        <TextView 
         android:text="@string/teacher" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:id="@+id/teacher" 
         android:layout_gravity="center_horizontal" 
         android:textColor="@android:color/black" 
         android:padding="5dp" 
         android:gravity="center" /> 
       </LinearLayout> 
      </android.support.v7.widget.CardView> 

      <android.support.v7.widget.CardView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:layout_margin="10dp" 
       android:id="@+id/ac"> 
       <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_margin="10dp"> 

        <ImageView 
         android:layout_width="match_parent" 
         android:layout_height="57dp" 
         app:srcCompat="@drawable/ic_account" 
         android:id="@+id/imageVie" /> 

        <TextView 
         android:text="@string/my_account" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:id="@+id/account" 
         android:layout_gravity="center_horizontal" 
         android:textColor="@android:color/black" 
         android:padding="5dp" 
         android:gravity="center" /> 
       </LinearLayout> 
      </android.support.v7.widget.CardView> 
     </LinearLayout> 
     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:layout_margin="10dp" 
      android:id="@+id/tools"> 

      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

       <ImageView 
        android:layout_width="match_parent" 
        android:layout_height="60dp" 
        app:srcCompat="@drawable/ic_tools" 
        android:id="@+id/imageView" 
        android:layout_margin="10dp" /> 

       <TextView 
        android:text="Tools" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/students" 
        android:layout_gravity="center_horizontal" 
        android:textColor="@android:color/black" 
        android:padding="5dp" 
        android:gravity="center" /> 
      </LinearLayout> 
     </android.support.v7.widget.CardView> 

    </LinearLayout> 
</ScrollView> 

它顯示例外

               java.lang.IllegalStateException: ScrollView can host only one direct child 
                           at android.widget.ScrollView.addView(ScrollView.java:237) 
                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1308) 
                           at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323) 
                           at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136) 
                           at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092) 
                           at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1998) 
                           at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:709) 
                           at android.os.Handler.handleCallback(Handler.java:733) 
                           at android.os.Handler.dispatchMessage(Handler.java:95) 
                           at android.os.Looper.loop(Looper.java:136) 
                           at android.app.ActivityThread.main(ActivityThread.java:5001) 
                           at java.lang.reflect.Method.invokeNative(Native Method) 
                           at java.lang.reflect.Method.invoke(Method.java:515) 
                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                           at dalvik.system.NativeStart.main(Native Method) 

我試圖清理的項目,並沒有幫助我

+0

是相同的活動?..你的XML不包含任何錯誤 –

+0

我試圖運行你的代碼,這是我的設備 – Umair

+0

分享自己的活動代碼 – Anjali

回答

1

是的,我找到了它。這是因爲我動態地用片段替換了內容。

fragmentTransaction.replace(R.id.content_home,new StudentFragment(),"student"); 
+0

現金:Rajesh和Nilabja –

+0

但片段只有一個家長!!!!!! –

+0

Upvote並接受我的答案,如果它幫助你。 –

1

你可能會加入一些觀點在你的java文件滾動型。所以不要添加任何視圖到滾動視圖。

scrollview.addView(someView); // Remove any code like this 
相關問題