ScrollView causes the App to crash

2019-09-05 22:30发布

问题:

I'm testing the some of the sensors available, and i display the values of each sensor in a textview.

But When I enclosed all the textviews inside a scrollView the App crashed.

Note: my scrollView is already enclosed inside a linearLayout

Please find below the .xml file.

XML:

<ScrollView
    android:isScrollContainer="true"
    android:scrollbars="vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

    <TextView
    android:id="@+id/accelerometer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="    Accelerometer  " />
<TextView
    android:id="@+id/acc_tv01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="X = " /> 
<TextView
    android:id="@+id/acc_tv01Value"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/acc_tv02"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Y = " />
<TextView
    android:id="@+id/acc_tv02Value"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/acc_tv03"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Z = " />
<TextView
    android:id="@+id/acc_tv03Value"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

    <TextView
    android:id="@+id/Orientation"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="    Orientation  " />
<TextView
    android:id="@+id/orient_tv01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="X = " /> 
<TextView
    android:id="@+id/orient_tv01Value"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/orient_tv02"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Y = " />
<TextView
    android:id="@+id/orient_tv02Value"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/orient_tv03"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Z = " />
<TextView
    android:id="@+id/orient_tv03Value"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

    <TextView
    android:id="@+id/Magnetometer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="    Magnetometer  " />
<TextView
    android:id="@+id/magnet_tv01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="X = " /> 
<TextView
    android:id="@+id/magnet_tv01Value"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/magnet_tv02"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Y = " />
<TextView
    android:id="@+id/magnet_tv02Value"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/magnet_tv03"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Z = " />
<TextView
    android:id="@+id/magnet_tv03Value"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

    <TextView
    android:id="@+id/Gyroscope"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="    Gyroscope  " />
<TextView
    android:id="@+id/gyro_tv01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="X = " /> 
<TextView
    android:id="@+id/gyro_tv01Value"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/gyro_tv02"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Y = " />
<TextView
    android:id="@+id/gyro_tv02Value"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/gyro_tv03"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Z = " />
<TextView
    android:id="@+id/gyro_tv03Value"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>

回答1:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
 android:background="@drawable/background"
android:orientation="vertical"
 >

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dip"
android:orientation="horizontal"
android:background="@drawable/navbar" >  

 <TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="50dip"
    android:gravity="left">        

 <Button  android:id="@+id/detailback"
 android:layout_width="70dip"
 android:layout_height="39dip"
 android:layout_marginTop="5dip"
 android:layout_marginLeft="5dip"
 android:background="@drawable/back"   
/>       
</TableRow>

</LinearLayout>

     <ScrollView
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:id="@+id/Scroll1"
   >


<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
 >

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_marginTop="20px"
android:layout_height="100px"

android:orientation="horizontal" 
 >

    <ImageView
        android:id="@+id/Dicimage"
        android:layout_width="130px"
        android:layout_height="100px"
        android:padding="1dp"/>


    <TextView 
        android:id="@+id/DictionaryName"
        android:textSize="25px"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_width="250px"
        android:layout_height="100px"
        android:textColor="#FFF"
        />

    <Button
        android:id="@+id/buy"
        android:background="@drawable/buy"
        android:text="Buy"
        android:textSize="20px"
        android:textStyle="bold"
        android:textColor="#FFF"
        android:layout_width="90px"
        android:layout_height="35px"


         />

 </LinearLayout>

    <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="350px"
android:layout_marginTop="20px"
android:layout_marginLeft="5px"
android:layout_marginRight="5px"
android:background="@drawable/box"
android:orientation="vertical"
 >
    <ScrollView
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/Scroll2"
     android:layout_width="fill_parent"
     android:fillViewport="true"
     android:layout_height="wrap_content"
   >

    <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="250px"
android:layout_marginTop="20px"
android:orientation="vertical"
 >
    <TextView 
        android:id="@+id/Description"
        android:textColor="#FFF"
        android:text="Description: "
        android:layout_marginLeft="15px"
        android:textSize="20px"
        android:textStyle="bold"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />


    <TextView 
        android:id="@+id/Description1"
        android:textColor="#FFF"
        android:layout_marginLeft="15px"
        android:textSize="20px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

   </LinearLayout>

     </ScrollView>

    </LinearLayout>



  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_marginTop="20px"
android:gravity="center"
android:layout_height="520px"
android:orientation="horizontal">

   <android.support.v4.view.ViewPager     
         android:layout_width="320px"
         android:layout_height="480px"
         android:id="@+id/myfivepanelpager"   
        />  

  </LinearLayout>


   </LinearLayout>

   </ScrollView>

    </LinearLayout>

here i used scrollview within another scrollview.it works fine..it may helps u