App crashes when put at end

2019-07-25 15:18发布

问题:

full file follows: trying to have full scroll vertical in android device but putting scroll view at end of file produces crash

   `<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
     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:layout_width="match_parent"
     android:layout_height="match_parent"
     android:paddingBottom="@dimen/activity_vertical_margin"
     android:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin"
     android:paddingTop="@dimen/activity_vertical_margin"
     app:layout_behavior="@string/appbar_scrolling_view_behavior"
     tools:context="com.cancunsteve.aboutcancunsteve.MainActivity"
     tools:showIn="@layout/activity_main">


    <ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView"
    android:layout_alignParentStart="true"
    android:layout_marginTop="56dp" />
    <ImageView android:id="@+id/imageView"
    android:layout_column="1"
    android:background="@mipmap/lpussy"
    android:contentDescription='android:ContentDescription="@string/desc"'
    tools:ignore="HardcodedText"
    android:layout_width="200dp"
    android:layout_height="230dp" />


    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="serving good people with special deals. "
    android:id="@+id/text1"
    android:layout_alignTop="@+id/imageView"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:background="#ffffff"
    android:foreground="#05ffffff"
    android:foregroundTint="#03ffffff"
    android:textColor="#000000" />



    <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText2"
    android:text="Cancun, Playa del Carmen, Chichen Itza."
    android:layout_below="@+id/text1"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

    <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:text="Holbox, Taxco, Palenque, Acapulco, Guanajuato,  
    Monterrey, Tenacatita, Mascota"
    android:layout_below="@+id/editText2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageButton"
    android:layout_below="@+id/editText3"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@mipmap/campa"
    tools:ignore="ContentDescription"
    android:layout_alignBottom="@+id/editText5" />

    <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:ems="10"
    android:id="@+id/editText3"
    android:text="thewebmaster@cancunsteve.com"
    android:layout_below="@+id/editText"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignRight="@+id/editText"
    android:layout_alignEnd="@+id/editText" />

    <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="phone"
    android:ems="10"
    android:id="@+id/editText4"
    android:text="       011 52 998 8873919"
    android:layout_below="@+id/editText3"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="42dp"
    android:layout_marginStart="32dp" />

    <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText5"
    android:text="be sure to visit us for discounts on your hotel bookings"
    android:layout_below="@+id/editText4"
    android:layout_toRightOf="@+id/scrollView"
    android:layout_toEndOf="@+id/scrollView" />

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="click buttons multiple times to change state"
    android:id="@+id/textView2"
    android:layout_below="@+id/imageButton" />


   </RelativeLayout>`

when ScrollView added after Relative layout file won't open put it in brackets proceeded with backslash / perhaps something I put in between the first and last ScrollView? I noticed in the examples you show, scrollview appears in the beginning.

回答1:

I reproduced your activity and after I removed the 2 backgrounds:

android:background="@mipmap/lpussy"
android:background="@mipmap/campa"

the activity does open fine.
Of course the scrollview is useless the way you use it, it must contain 1 container layout and inside that container all the views that you want to scroll.
Remove the backgrounds as well and try again. Maybe the problem is there.



回答2:

<ScrollView xmlns: android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

your views

        </LinearLayout>
</ScrollView>

as scroll view can only contain a sinle child , so wrapping your views in a layout will work


回答3:

    <?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:layout_width="match_parent"
     android:layout_height="match_parent"
     android:paddingBottom="@dimen/activity_vertical_margin"
     android:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin"
     android:paddingTop="@dimen/activity_vertical_margin"
     app:layout_behavior="@string/appbar_scrolling_view_behavior"
     tools:context="com.cancunsteve.aboutcancunsteve.MainActivity"
     tools:showIn="@layout/activity_main">


<RelativeLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
    android:layout_marginTop="56dp" >
    <ImageView android:id="@+id/imageView"
    android:layout_column="1"
    android:background="@mipmap/lpussy"
    android:contentDescription='android:ContentDescription="@string/desc"'
    tools:ignore="HardcodedText"
    android:layout_width="200dp"
    android:layout_height="230dp" />


    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="serving good people with special deals. "
    android:id="@+id/text1"
    android:layout_alignTop="@+id/imageView"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:background="#ffffff"
    android:foreground="#05ffffff"
    android:foregroundTint="#03ffffff"
    android:textColor="#000000" />



    <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText2"
    android:text="Cancun, Playa del Carmen, Chichen Itza."
    android:layout_below="@+id/text1"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

    <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:text="Holbox, Taxco, Palenque, Acapulco, Guanajuato,  
    Monterrey, Tenacatita, Mascota"
    android:layout_below="@+id/editText2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageButton"
    android:layout_below="@+id/editText3"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@mipmap/campa"
    tools:ignore="ContentDescription"
    android:layout_alignBottom="@+id/editText5" />

    <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:ems="10"
    android:id="@+id/editText3"
    android:text="thewebmaster@cancunsteve.com"
    android:layout_below="@+id/editText"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignRight="@+id/editText"
    android:layout_alignEnd="@+id/editText" />

    <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="phone"
    android:ems="10"
    android:id="@+id/editText4"
    android:text="       011 52 998 8873919"
    android:layout_below="@+id/editText3"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="42dp"
    android:layout_marginStart="32dp" />

    <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText5"
    android:text="be sure to visit us for discounts on your hotel bookings"
    android:layout_below="@+id/editText4"
    android:layout_toRightOf="@+id/scrollView"
    android:layout_toEndOf="@+id/scrollView" />

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="click buttons multiple times to change state"
    android:id="@+id/textView2"
    android:layout_below="@+id/imageButton" />


   </RelativeLayout>
</ScrollView>


回答4:

ScrollView must have a single child.

If you want your full layout to be scrollable then add at the top.

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_grey"
    android:orientation="vertical">


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

            <TextView
                android:id="@+id/text1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignTop="@+id/imageView"
                android:background="#ffffff"
                android:foreground="#05ffffff"
                android:foregroundTint="#03ffffff"
                android:text="serving good people with special deals. "
                android:textColor="#000000" />


            <EditText
                android:id="@+id/editText2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/text1"
                android:text="Cancun, Playa del Carmen, Chichen Itza." />

            <EditText
                android:id="@+id/editText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/editText2"
                android:text="Holbox, Taxco, Palenque, Acapulco, Guanajuato,
    Monterrey, Tenacatita, Mascota" />

            <ImageButton
                android:id="@+id/imageButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/editText5"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/editText3"
                android:background="@mipmap/ic_launcher"
                tools:ignore="ContentDescription" />

            <EditText
                android:id="@+id/editText3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignEnd="@+id/editText"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignRight="@+id/editText"
                android:layout_below="@+id/editText"
                android:ems="10"
                android:inputType="textEmailAddress"
                android:text="thewebmaster@cancunsteve.com" />

            <EditText
                android:id="@+id/editText4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/editText3"
                android:layout_marginLeft="42dp"
                android:layout_marginStart="32dp"
                android:ems="10"
                android:inputType="phone"
                android:text="       011 52 998 8873919" />

            <EditText
                android:id="@+id/editText5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/editText4"
                android:layout_toEndOf="@+id/scrollView"
                android:layout_toRightOf="@+id/scrollView"
                android:text="be sure to visit us for discounts on your hotel bookings" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/imageButton"
                android:text="click buttons multiple times to change state" />


        </LinearLayout>

    </ScrollView>


</RelativeLayout>


回答5:

To make RelativeLayout scrollable, put it inside LinearLayout and then put that LinearLayout inside ScrollView. This will solve the problem.

Use the format shown below.

<ScrollView xmlns: android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

            ...
            ...
            ...

        </RelativeLayout>
    </LinearLayout>
</ScrollView>

UPDATE:

Step 1:

 <ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView"
    android:layout_alignParentStart="true"
    android:layout_marginTop="56dp">       <---- LOOK HERE (remove /)

Step 2:

     </ScrollView>    <---- LOOK HERE (end the ScrollView before RelativeLayout)
</RelativeLayout>


回答6:

As per your xml, Its better to swap the top two element tags in your xml file.

So that yor RelativeLayout will become a child of ScrollView. and You dont have to edit your remaining layout elements

The result will be..

< 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:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/scrollView"
     android:paddingBottom="@dimen/activity_vertical_margin"
     android:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin"
     android:paddingTop="@dimen/activity_vertical_margin"
     app:layout_behavior="@string/appbar_scrolling_view_behavior"
     tools:context="com.cancunsteve.aboutcancunsteve.MainActivity"
     tools:showIn="@layout/activity_main">




    <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_marginTop="56dp" >

And it should end like..

</RelativeLayout>
</ScrollView>

Addition: You can replace the Relative Layout with a LinearLayout to get Rid of scrollView Errors...

<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/scrollView">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:orientation="vertical"
        android:layout_marginTop="56dp" >

    <ImageView android:id="@+id/imageView"
        android:layout_column="1"
        android:background="#ff0f"
        android:contentDescription='android:ContentDescription="@string/desc"'
        tools:ignore="HardcodedText"
        android:layout_width="200dp"
        android:layout_height="230dp" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="serving good people with special deals. "
        android:id="@+id/text1"
        android:layout_alignTop="@+id/imageView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="#ffffff"
        android:foreground="#05ffffff"
        android:foregroundTint="#03ffffff"
        android:textColor="#000000" />



    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText2"
        android:text="Cancun, Playa del Carmen, Chichen Itza."
        android:layout_below="@+id/text1"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:text="Holbox, Taxco, Palenque, Acapulco, Guanajuato,
    Monterrey, Tenacatita, Mascota"
        android:layout_below="@+id/editText2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton"
        android:layout_below="@+id/editText3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="#f0f0"
        tools:ignore="ContentDescription"
        android:layout_alignBottom="@+id/editText5" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/editText3"
        android:text="thewebmaster@cancunsteve.com"
        android:layout_below="@+id/editText"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/editText"
        android:layout_alignEnd="@+id/editText" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:ems="10"
        android:id="@+id/editText4"
        android:text="       011 52 998 8873919"
        android:layout_below="@+id/editText3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="42dp"
        android:layout_marginStart="32dp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText5"
        android:text="be sure to visit us for discounts on your hotel bookings"
        android:layout_below="@+id/editText4" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click buttons multiple times to change state"
        android:id="@+id/textView2"
        android:layout_below="@+id/imageButton" />

    </LinearLayout>

</ScrollView>