我每次開始活動時都會收到此錯誤。ScrollView只能託管一個直接孩子,但它只有一個
下面是完整的堆棧跟蹤:
Process: com.example, PID: 28799
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.activity.EditProjectActivity}: java.lang.IllegalStateException: ScrollView can host only one direct child
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2689)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2754)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5938)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child
at android.widget.ScrollView.addView(ScrollView.java:416)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:822)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.activity.EditProjectActivity.onCreate(EditProjectActivity.java:38)
at android.app.Activity.performCreate(Activity.java:6288)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2642)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2754)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5938)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
而這正是佈局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".activity.EditProjectActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:id="@+id/ep_layout_name"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project Name: "/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:id="@+id/ep_edit_name"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/ep_layout_name"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:id="@+id/ep_layout_categories"
>
<ListView
android:id="@+id/ep_list_categories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/ep_btn_add_category"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Add"
android:onClick="onAddCategoryButtonClick"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/ep_layout_categories"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:id="@+id/ep_layout_versions"
>
<ListView
android:id="@+id/ep_list_versions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/ep_btn_add_version"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Add"
android:onClick="onAddVersionButtonClick"
/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
我明白什麼錯誤意味着,我嘗試了相應解決它,但它仍然拋出相同的異常。
ScrollView只有一個直接孩子,那就是RelativeLayout。我也做了這樣的ScrollView有一個父母,並不是基礎視圖,以防萬一它引起的異常,但它沒有工作。
再次添加
LinearLayout
你這個滾動視圖中的代碼進行交互? –試過你的佈局,跑了OK。有你的嘗試清潔項目。我們可能需要更多信息。 –