每当我开始活动时,我都会收到此错误。
这里是完整的堆栈跟踪:
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有一个父级,并且不是基本视图,以防它导致异常,但它没有工作。
答案 0 :(得分:2)
可能会尝试在LinearLayout
vertical
方向添加RelativeLayout
<?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"
>
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
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>
</LinearLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
答案 1 :(得分:1)
专注
附加屏幕快照以供参考。...
答案 2 :(得分:0)
您可能会在java文件中向scrollview添加一些视图。所以不要向scrollview添加任何视图。
scrollview.addView(someView); // Remove any code like this