0
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:text="Toppings"
            android:textAllCaps="true" />

        <CheckBox
            android:id="@+id/whipped_cream_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="24dp"
            android:text="Whipped Cream"
            android:textSize="16sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginTop="16dp"
            android:text="Quantity"
            android:textAllCaps="true" />

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

            <Button
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:onClick="decrement"
                android:text="-" />

            <TextView
                android:id="@+id/quantity_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:text="0"
                android:textColor="#000000"
                android:textSize="16sp" />

            <Button
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:onClick="increment"
                android:text="+" />

        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginTop="16dp"
            android:text="Order Summary"
            android:textAllCaps="true" />

        <TextView
            android:id="@+id/order_summary_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="$0"
            android:textColor="#000000"
            android:textSize="16sp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:onClick="submitOrder"
            android:text="Order" />

    </LinearLayout>
</ScrollView>

Without the ScrollView, the code works just fine, but when I added it, it started crashing my app once I try to start it. I am coding on Udacity and followed everything that the teacher did, but my code is not working. Can anyone help me?

Edit: Here is the logcat test at the time of the crash. It says that ScrollView can only have one direct child, but from what I can see, mine does have only one direct child.

09-06 14:07:26.524 13976-13976/com.example.android.justjava E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.justjava, PID: 13976
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.justjava/com.example.android.justjava.MainActivity}: 
java.lang.IllegalStateException: 
ScrollView can host only one direct child
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495) 
at android.app.ActivityThread.access$900(ActivityThread.java:170)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: 
ScrollView can host only one direct child
at android.widget.ScrollView.addView(ScrollView.java:410)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:770)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.android.justjava.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:5585)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495) 
at android.app.ActivityThread.access$900(ActivityThread.java:170) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:146) 
at android.app.ActivityThread.main(ActivityThread.java:5635) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 
at dalvik.system.NativeStart.main(Native Method) 
Dale Wilson
  • 9,166
  • 3
  • 34
  • 52
  • 1
    Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Sep 06 '16 at 21:16
  • Please post the exception here. – zsolt.kocsi Sep 06 '16 at 21:22
  • Excellent course, I completed this recently, what is the exact error message? – ShadowGod Sep 06 '16 at 21:26
  • Thanks for posting the LogCat. That helps. Now please show the code at `com.example.android.justjava.MainActivity.onCreate(MainActivity.java:23)` and verify that the layout being added is the one that you have shown in is question (Check your resource ID's vs layout file names) – Dale Wilson Sep 06 '16 at 21:45
  • 1
    I'm not seeing anything wrong, there is only one child view .. did you try Build --> Clean Project ? – ShadowGod Sep 06 '16 at 21:49
  • I did Clean Project and it worked!! I'm not sure why, but it did. Thanks guys!! – Patrick Porter Sep 06 '16 at 22:07
  • Can you try to use android:layout_width="wrap_content" instead of "match_parent" for the topmost LinearLayout? (Both android:layout_width and android:layout_height should be set to "wrap_content"). Let me know if it fixes the problem. – Hungry Coder Sep 06 '16 at 22:27

0 Answers0