我有一个带有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 :(得分:1)
您可能会在java文件中向scrollview添加一些视图。所以不要向scrollview添加任何视图。
scrollview.addView(someView); // Remove any code like this
答案 1 :(得分:1)
是的我找到了它。因为我用片段动态替换了内容。
fragmentTransaction.replace(R.id.content_home,new StudentFragment(),"student");