添加scrollview崩溃的应用程序

时间:2014-02-03 06:22:02

标签: android xml

我是一个为情人节创建应用程序的任务,我正在制作情书生成器。我只能使用一个活动,所以我创建了一个textview集可见性。情书有点长,所以我想要滚动视图。添加崩溃的应用程序。请帮忙

 <EditText
    android:id="@+id/ename"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="42dp"
    android:hint="@string/name"
    android:inputType="textPersonName" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/epname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/ename"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="42dp"
    android:hint="@string/pname" >
</EditText>

<Button
    android:id="@+id/bnext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="@string/nxt" />

<Button
    android:id="@+id/email"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="@string/Email"
    android:visibility="invisible" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/bnext"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignStart="@id/bnext"
    android:visibility="gone" />

<TextView
    android:id="@+id/prmt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/epname"
    android:layout_marginTop="42dp"
    android:text="@string/Prompt"
    android:textSize="20sp" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/prmt"
    android:layout_marginTop="22dp"
    android:entries="@array/opts" />

这很好用 我将滚动视图添加到文本view1

 <ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/bnext"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignStart="@id/bnext" >

    <TextView
        android:id="@+id/textView1"
        android:visibility="gone" />
</ScrollView>

这次崩溃。 logcat的

> 02-03 12:14:05.191: E/AndroidRuntime(30264): FATAL EXCEPTION: main
02-03 12:14:05.191: E/AndroidRuntime(30264): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.assignment3/com.example.assignment3.MainActivity}: java.lang.RuntimeException: Binary XML file line #63: You must supply a layout_width attribute.
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2253)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2288)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.app.ActivityThread.access$600(ActivityThread.java:144)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1290)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.os.Looper.loop(Looper.java:156)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.app.ActivityThread.main(ActivityThread.java:5109)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at java.lang.reflect.Method.invokeNative(Native Method)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at java.lang.reflect.Method.invoke(Method.java:511)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:991)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at dalvik.system.NativeStart.main(Native Method)
02-03 12:14:05.191: E/AndroidRuntime(30264): Caused by: java.lang.RuntimeException: Binary XML file line #63: You must supply a layout_width attribute.
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:5665)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:5786)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1776)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1700)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:56)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:741)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:276)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.app.Activity.setContentView(Activity.java:1897)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at com.example.assignment3.MainActivity.onCreate(MainActivity.java:28)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.app.Activity.performCreate(Activity.java:4543)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
02-03 12:14:05.191: E/AndroidRuntime(30264):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
02-03 12:14:05.191: E/AndroidRuntime(30264):    ... 11 more

TIA

4 个答案:

答案 0 :(得分:5)

您收到该错误是因为您没有为width提供任何heightTextView 在你的布局中。布局应该是这样的

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_above="@id/bnext"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@id/bnext" >
    <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:visibility="gone"
                android:orientation="vertical" >
        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    </ScrollView>

android:visibility="gone"parent LinearLayout

设置此属性

答案 1 :(得分:2)

滚动视图必须包含所有子视图的父视图组。文本视图不是视图组。将“线性布局”或“相对布局”添加为“滚动视图”内部文本视图的父级。有关详细信息,请参阅此http://developer.android.com/reference/android/widget/ScrollView.html。使用以下布局,您的问题将得到解决。

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/bnext"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignStart="@id/bnext" >

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

        <TextView
            android:id="@+id/textView1"
            android:visibility="gone" />
    </LinearLayout>
</ScrollView>

答案 2 :(得分:1)

AFAIK您必须在ScrollView内找到一个视图,但您无法直接在ScrollView内添加视图,而您需要添加一些布局,即LinearLayoutRelativeLayout ..在你的ScrollView和你的布局中,你可以添加你的观点。

不允许在ScrollView中添加直接子广告或视图。

尝试如下:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/bnext"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignStart="@id/bnext" >
  <RelativeLayout
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
     >

 <TextView
            android:id="@+id/textView1"
            android:visibility="gone" />
    </RelativeLayout>

</ScrollView>

答案 3 :(得分:1)

在编码中添加它将起作用

     <ScrollView
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_above="@id/bnext"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_alignStart="@id/bnext" >
        <LinearLayout
                    android:id="@+id/linearLayout1"
                    android:layout_width="match_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center_vertical|center_horizontal"
                    android:orientation="vertical" >
            <TextView
                android:id="@+id/textView1"
                android:visibility="gone"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
        </ScrollView>