首页 > 解决方案 > ListView crashes

问题描述

I implemented a listview but when i run my code app crash and showing nothing please look at my code and define me what is the error of this problem.

 ListView lst;
String[]  tools  = {"Dreamweaver", "Sublime", "Hardware"};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tools);

    lst = (ListView) findViewById(R.id.simplelist);
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,tools);
    lst.setAdapter(arrayAdapter);
}

-------------------Tools Activity -------------------

<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@drawable/materialbg"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/simplelist"></ListView>
<LinearLayout 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"
    tools:context=".skills"
    android:orientation="vertical"
    android:theme="@style/AppTheme1"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:orientation="vertical"
        android:gravity="center">

        <ImageView
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:src="@drawable/tool"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TOOLS"
            android:textSize="25dp"
            android:textStyle="bold"
            android:textColor="@color/colorLight"
            android:layout_marginTop="15dp"/>


    </LinearLayout>

    <!--<include layout="@layout/toolbar" android:id="@+id/toolbar"/>-->
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_margin="5dp"
        android:layout_marginBottom="10dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:weightSum="5"

            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="HTML"
                android:background="@color/colorWhite"
                android:textColor="@color/colorPrimaryDark"
                android:layout_weight="4"
                android:gravity="center_vertical"
                android:textAlignment="gravity"
                android:paddingLeft="20dp"
                android:textSize="8pt"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="95%"
                android:textColor="@color/colorAccent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:textAlignment="center"
                android:textSize="8pt"
                android:textStyle="bold"

                />

        </LinearLayout>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_margin="5dp"
        android:layout_marginBottom="10dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:weightSum="5"

            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="CSS"
                android:background="@color/colorWhite"
                android:textColor="@color/colorPrimaryDark"
                android:layout_weight="4"
                android:gravity="center_vertical"
                android:textAlignment="gravity"
                android:paddingLeft="20dp"
                android:textSize="8pt"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="80%"
                android:textColor="@color/colorAccent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:textAlignment="center"
                android:textSize="8pt"
                android:textStyle="bold"

                />

        </LinearLayout>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_margin="5dp"
        android:layout_marginBottom="10dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:weightSum="5"

            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="PHP"
                android:background="@color/colorWhite"
                android:textColor="@color/colorPrimaryDark"
                android:layout_weight="4"
                android:gravity="center_vertical"
                android:textAlignment="gravity"
                android:paddingLeft="20dp"
                android:textSize="8pt"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="60%"
                android:textColor="@color/colorAccent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:textAlignment="center"
                android:textSize="8pt"
                android:textStyle="bold"

                />

        </LinearLayout>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_margin="5dp"
        android:layout_marginBottom="10dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:weightSum="5"

            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Laravel"
                android:background="@color/colorWhite"
                android:textColor="@color/colorPrimaryDark"
                android:layout_weight="4"
                android:gravity="center_vertical"
                android:textAlignment="gravity"
                android:paddingLeft="20dp"
                android:textSize="8pt"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="40%"
                android:textColor="@color/colorAccent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:textAlignment="center"
                android:textSize="8pt"
                android:textStyle="bold"

                />

        </LinearLayout>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_margin="5dp"
        android:layout_marginBottom="10dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:weightSum="5"

            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Angular JS"
                android:background="@color/colorWhite"
                android:textColor="@color/colorPrimaryDark"
                android:layout_weight="4"
                android:gravity="center_vertical"
                android:textAlignment="gravity"
                android:paddingLeft="20dp"
                android:textSize="8pt"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="30%"
                android:textColor="@color/colorAccent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:textAlignment="center"
                android:textSize="8pt"
                android:textStyle="bold"

                />

        </LinearLayout>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_margin="5dp"
        android:layout_marginBottom="10dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:weightSum="5"

            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Android Development"
                android:background="@color/colorWhite"
                android:textColor="@color/colorPrimaryDark"
                android:layout_weight="4"
                android:gravity="center_vertical"
                android:textAlignment="gravity"
                android:paddingLeft="20dp"
                android:textSize="8pt"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="30%"
                android:textColor="@color/colorAccent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:textAlignment="center"
                android:textSize="8pt"
                android:textStyle="bold"

                />

        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

--------------------- Log Cat ------------------

06-02 10:37:26.492 1610-1683/system_process E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
06-02 10:37:26.492 1610-1683/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
06-02 10:37:26.492 1610-1683/system_process E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
06-02 10:37:26.492 1610-1683/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
06-02 10:37:26.492 1610-1683/system_process E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
06-02 10:37:26.492 1610-1683/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
06-02 10:37:26.492 1610-1683/system_process E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
06-02 10:37:26.492 1610-1683/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
06-02 10:37:26.492 1610-1683/system_process E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
06-02 10:37:26.492 1610-1683/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
06-02 10:37:26.492 1610-1683/system_process E/SoundPool: error loading /system/media/audio/ui/KeypressStandard.ogg
06-02 10:37:26.492 1610-1683/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
06-02 10:37:26.492 1610-1683/system_process E/SoundPool: error loading /system/media/audio/ui/KeypressSpacebar.ogg
06-02 10:37:26.492 1610-1683/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
06-02 10:37:26.492 1610-1683/system_process E/SoundPool: error loading /system/media/audio/ui/KeypressDelete.ogg
06-02 10:37:26.492 1610-1683/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
06-02 10:37:26.492 1610-1683/system_process E/SoundPool: error loading /system/media/audio/ui/KeypressReturn.ogg
06-02 10:37:26.492 1610-1683/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
06-02 10:37:26.492 1610-1683/system_process E/SoundPool: error loading /system/media/audio/ui/KeypressInvalid.ogg
06-02 10:37:26.492 1610-1683/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
    onLoadSoundEffects(), Error -1 while loading samples

--------- beginning of /dev/log/system

06-02 10:37:26.492 1610-2544/system_process I/ActivityManager: START u0 {cmp=com.poetryappp.com.faltoproject/.stage} from pid 3184
06-02 10:37:26.512 3184-3184/com.poetryappp.com.faltoproject I/dalvikvm: Could not find method android.widget.FrameLayout.startActionModeForChild, referenced from method android.support.v7.widget.ActionBarContainer.startActionModeForChild
06-02 10:37:26.512 3184-3184/com.poetryappp.com.faltoproject W/dalvikvm: VFY: unable to resolve virtual method 16724: Landroid/widget/FrameLayout;.startActionModeForChild (Landroid/view/View;Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
06-02 10:37:26.512 3184-3184/com.poetryappp.com.faltoproject D/dalvikvm: VFY: replacing opcode 0x6f at 0x0002
06-02 10:37:26.552 3184-3184/com.poetryappp.com.faltoproject D/dalvikvm: GC_FOR_ALLOC freed 84K, 1% free 61642K/61916K, paused 5ms, total 7ms
06-02 10:37:26.572 3184-3184/com.poetryappp.com.faltoproject D/dalvikvm: GC_FOR_ALLOC freed 10K, 1% free 63537K/63812K, paused 3ms, total 4ms
06-02 10:37:26.582 3184-3184/com.poetryappp.com.faltoproject D/dalvikvm: GC_FOR_ALLOC freed 1K, 1% free 65306K/65580K, paused 3ms, total 4ms
06-02 10:37:26.612 3184-3184/com.poetryappp.com.faltoproject D/dalvikvm: GC_FOR_ALLOC freed 1K, 1% free 67074K/67348K, paused 5ms, total 6ms
06-02 10:37:26.632 3184-3184/com.poetryappp.com.faltoproject D/dalvikvm: GC_FOR_ALLOC freed <1K, 1% free 68842K/69116K, paused 4ms, total 4ms
06-02 10:37:26.652 3184-3184/com.poetryappp.com.faltoproject D/dalvikvm: GC_FOR_ALLOC freed <1K, 1% free 70611K/70884K, paused 5ms, total 5ms
06-02 10:37:26.742 3184-3184/com.poetryappp.com.faltoproject D/EGL_emulation: eglMakeCurrent: 0xb89c0730: ver 3 1
06-02 10:37:26.742 3184-3184/com.poetryappp.com.faltoproject E/EGL_emulation: tid 3184: eglSurfaceAttrib(1199): error 0x3009 (EGL_BAD_MATCH)
06-02 10:37:26.742 3184-3184/com.poetryappp.com.faltoproject W/HardwareRenderer: Backbuffer cannot be preserved
06-02 10:37:26.762 3184-3184/com.poetryappp.com.faltoproject I/dalvikvm: Could not find method android.view.View.setTooltipText, referenced from method android.support.v7.widget.TooltipCompat.setTooltipText
06-02 10:37:26.762 3184-3184/com.poetryappp.com.faltoproject W/dalvikvm: VFY: unable to resolve virtual method 16039: Landroid/view/View;.setTooltipText (Ljava/lang/CharSequence;)V
06-02 10:37:26.762 3184-3184/com.poetryappp.com.faltoproject D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
06-02 10:37:27.452 1610-1630/system_process I/ActivityManager: Displayed com.poetryappp.com.faltoproject/.stage: +957ms
06-02 10:37:27.762 3184-3184/com.poetryappp.com.faltoproject D/EGL_emulation: eglMakeCurrent: 0xb89c0730: ver 3 1
06-02 10:37:27.792 3184-3184/com.poetryappp.com.faltoproject D/EGL_emulation: eglMakeCurrent: 0xb89c0730: ver 3 1
06-02 10:37:27.892 3184-3184/com.poetryappp.com.faltoproject D/EGL_emulation: eglMakeCurrent: 0xb89c0730: ver 3 1
06-02 10:37:28.442 1610-1683/system_process E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
06-02 10:37:28.442 1610-1683/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
06-02 10:37:28.442 1610-1683/system_process E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
06-02 10:37:28.442 1610-1683/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
06-02 10:37:28.442 1610-1683/system_process E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
06-02 10:37:28.442 1610-1683/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
06-02 10:37:28.442 1610-1683/system_process E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
06-02 10:37:28.442 1610-1683/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
06-02 10:37:28.442 1610-1683/system_process E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
06-02 10:37:28.442 1610-1683/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
06-02 10:37:28.442 1610-1683/system_process E/SoundPool: error loading /system/media/audio/ui/KeypressStandard.ogg
06-02 10:37:28.442 1610-1683/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
06-02 10:37:28.442 1610-1683/system_process E/SoundPool: error loading /system/media/audio/ui/KeypressSpacebar.ogg
06-02 10:37:28.442 1610-1683/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
06-02 10:37:28.442 1610-1683/system_process E/SoundPool: error loading /system/media/audio/ui/KeypressDelete.ogg
06-02 10:37:28.442 1610-1683/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
06-02 10:37:28.442 1610-1683/system_process E/SoundPool: error loading /system/media/audio/ui/KeypressReturn.ogg
06-02 10:37:28.442 1610-1683/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
06-02 10:37:28.442 1610-1683/system_process E/SoundPool: error loading /system/media/audio/ui/KeypressInvalid.ogg
06-02 10:37:28.442 1610-1683/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
    onLoadSoundEffects(), Error -1 while loading samples
06-02 10:37:28.442 1610-2538/system_process I/ActivityManager: START u0 {cmp=com.poetryappp.com.faltoproject/.tools} from pid 3184
06-02 10:37:28.482 3184-3184/com.poetryappp.com.faltoproject D/AndroidRuntime: Shutting down VM
06-02 10:37:28.482 3184-3184/com.poetryappp.com.faltoproject W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x9ccbbb20)
06-02 10:37:28.492 3184-3184/com.poetryappp.com.faltoproject D/dalvikvm: GC_FOR_ALLOC freed 131K, 1% free 72526K/72848K, paused 7ms, total 9ms
06-02 10:37:28.492 3184-3184/com.poetryappp.com.faltoproject E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.poetryappp.com.faltoproject, PID: 3184
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.poetryappp.com.faltoproject/com.poetryappp.com.faltoproject.tools}: java.lang.IllegalStateException: ScrollView can host only one direct child
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        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:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        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:255)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
        at com.poetryappp.com.faltoproject.tools.onCreate(tools.java:20)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
        at android.app.ActivityThread.access$800(ActivityThread.java:135) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:136) 
        at android.app.ActivityThread.main(ActivityThread.java:5017) 
        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:779) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
        at dalvik.system.NativeStart.main(Native Method) 
06-02 10:37:28.502 1610-2540/system_process W/ActivityManager:   Force finishing activity com.poetryappp.com.faltoproject/.tools
      Force finishing activity com.poetryappp.com.faltoproject/.stage
06-02 10:37:28.552 1134-1899/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 1114112
06-02 10:37:29.012 1769-1769/com.android.launcher D/EGL_emulation: eglMakeCurrent: 0xb89e8e70: ver 3 1
06-02 10:37:29.012 1610-1631/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{9cfc6e30 u0 com.poetryappp.com.faltoproject/.tools t6 f}
06-02 10:37:29.042 1769-1769/com.android.launcher D/EGL_emulation: eglMakeCurrent: 0xb89e8e70: ver 3 1
06-02 10:37:29.042 1769-1769/com.android.launcher E/EGL_emulation: tid 1769: eglSurfaceAttrib(1199): error 0x3009 (EGL_BAD_MATCH)
06-02 10:37:29.042 1769-1769/com.android.launcher W/HardwareRenderer: Backbuffer cannot be preserved
06-02 10:37:29.532 1134-1899/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 1114112
06-02 10:37:29.742 1134-1906/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 1114112
06-02 10:37:39.152 1610-1631/system_process W/ActivityManager: Activity destroy timeout for ActivityRecord{9d462390 u0 com.poetryappp.com.faltoproject/.stage t6 f}
    Activity destroy timeout for ActivityRecord{9cfc6e30 u0 com.poetryappp.com.faltoproject/.tools t6 f}

标签: javaandroid

解决方案


根据您发布的异常,您的布局中似乎有多个视图是 ScrollView 的子视图。

ScrollView 只能承载一个子视图。

如果你想在一个 ScrollView 中有多个视图,你应该把你想滚动的视图放在一个 ViewGroup 里面,比如一个 LinearLayout。然后,将此 LinearLayout 设为 ScrollView 的唯一子项。

就像是:

<ScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="60dp">
    <LinearLayout
        android:id="@+id/single_view_group_in_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="A TextView in a ScrollView" />
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="200dp"
            android:text="A tall Button in a ScrollView" />
    </LinearLayout>
</ScrollView>

推荐阅读