Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 设置ListView背景时应用程序崩溃_Java_Android_Eclipse_Listview - Fatal编程技术网

Java 设置ListView背景时应用程序崩溃

Java 设置ListView背景时应用程序崩溃,java,android,eclipse,listview,Java,Android,Eclipse,Listview,我正在尝试为我的应用程序创建一种卡片布局。在activity_events下,我需要将list_行设置为列表中每个项目的背景。如下所示,应用程序在调用活动时崩溃。但是仅仅删除ListView-android:background=“@layout/list\u行”会导致一切正常,只是看起来不对劲。我已经尝试将列表行移动到drawable文件夹并使用@drawable/list\u row,但也没有成功。列表视图中的背景是否有一些我不理解的限制 下面是activity\u events.xml &

我正在尝试为我的应用程序创建一种卡片布局。在activity_events下,我需要将list_行设置为列表中每个项目的背景。如下所示,应用程序在调用活动时崩溃。但是仅仅删除
ListView-android:background=“@layout/list\u行”
会导致一切正常,只是看起来不对劲。我已经尝试将列表行移动到drawable文件夹并使用
@drawable/list\u row
,但也没有成功。列表视图中的背景是否有一些我不理解的限制

下面是
activity\u events.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" >

    <ListView
            android:id="@+id/android:list"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null"    
        android:dividerHeight="0dp"
        android:background="@layout/list_row">
    </ListView>

    <ProgressBar
        android:id="@+id/eventsProgressBar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true" />

    </LinearLayout>

下面是
list\u selector.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left side Thumbnail image -->
<LinearLayout
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="5dip"
    android:padding="3dip" 
    android:orientation="vertical">
   <ImageView
        android:id="@+id/list_image"
        android:contentDescription="@string/app_name"
        android:layout_width="60dip"
        android:layout_height="60dip"
        android:src="@drawable/barjinx_logo" /> <!--Removal makes it work!-->
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:state_selected="false"
    android:state_pressed="false"
    android:drawable="@drawable/gradient_bg" />
<item android:state_pressed="true"
    android:drawable="@drawable/gradient_bg_hover" />
<item android:state_selected="true"
    android:state_pressed="false"
    android:drawable="@drawable/gradient_bg_hover" />


不能将布局用作背景。只能使用
颜色
可绘制

行布局在您的
ListAdapter.getView()
中设置

编辑

您的ListView应该如下所示:

<ListView
    android:id="@+id/android:list"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@null"    
    android:dividerHeight="0dp"
    android:listSelector="@drawable/list_selector">
</ListView>


要设置列表行的样式,需要创建一个
ListAdapter
。由于您使用的是自定义布局,因此需要扩展
ArrayAdapter
BaseAdapter
,然后在
ListView
上调用
ListView.setListAdapter(ListAdapter)
。查看Vogella。

您不必在
列表视图上设置背景,您需要在
列表视图上设置一个适配器(扩展
BaseAdapter
),并让它从
getView()
返回布局(您可以使用
LayoutInflater
类为
R.layout.list行


编辑:更多信息

发布异常报告我的回答对你有帮助吗?那么,如果不制作庞然大物布局,我就无法将一个布局嵌套在另一个布局中吗?我确实试过把它当作一个可拉丝的,但它似乎不喜欢这样。“android:src”是更好的选择吗?实际上我对布局了解不多,这方面很新。请查看更新的答案,并确保查看链接的文章。