3

我正在尝试使用本教程中的这个LazyAdapter类填充自定义 ListView

我想用自定义设计制作列表视图项目。布局文件list_row.xml在我的布局文件中,就像在教程中一样。这是类中稍作修改的 getView 函数LazyAdapter

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        //vi = inflater.inflate(R.layout.list_row, null);    //EDITED
        vi = inflater.inflate(R.layout.list_row, parent, false);
    TextView title = (TextView)vi.findViewById(R.id.title); // title
    TextView subtitle = (TextView)vi.findViewById(R.id.subtitle); // artist name
    //ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);

    //This is not needed
    //HashMap<String, String> post = new HashMap<String, String>();
    HashMap<String, String> post;
    post = data.get(position);
    // Setting all values in listview
    title.setText(post.get("title"));     //the code crashes here
    subtitle.setText(post.get("subtitle"));
    //imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
    return vi;
}

好吧,这段代码在 处崩溃title.setText(),因为标题为空。我在某处读到这是有道理的,因为这个 LazyAdapterfindViewById之前正在调用setContentView或其他什么..

那么我该怎么做呢?这段代码如何为那个人工作?

更新

这是list_row.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" >

    <!-- Title -->
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#040404"
        android:typeface="sans"
        android:textSize="14sp"
        android:textStyle="bold"/>

    <!-- Subtitle -->
    <TextView
        android:id="@+id/subtitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/subtitle"
        android:textColor="#343434"
        android:textSize="10sp"
        android:layout_marginTop="1dip" />

</RelativeLayout>

为了节省空间,我只ListView从主活动布局文件中添加 xml:

<ListView
    android:id="@+id/feedListView"
    android:layout_marginTop="20dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/volumeControl1">
</ListView>

适配器代码: http: //pastebin.com/niC1yXiJ

和堆栈跟踪: http: //pastebin.com/myW8B0Xz

4

3 回答 3

1

这是一个地狱般的旅程(两个不眠之夜)。感谢您的所有帮助,不知何故我修复了它!:)

主要归功于这个问题Null pointer exception in getView() of custom Adapter我创建了新的 getView 函数:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = new ViewHolder();
    View vi = convertView;
    if (vi == null) {
        LayoutInflater inflater = ((Activity)activity).getLayoutInflater();
        vi = inflater.inflate(R.layout.list_row, null);
        holder.title = (TextView) vi.findViewById(R.id.title);
        holder.subtitle = (TextView) vi.findViewById(R.id.subtitle);
        vi.setTag(holder);
    } else {

        holder = (ViewHolder) vi.getTag();
    }
    HashMap<String, String> post;
    post = data.get(position);
    holder.title.setText(post.get("title"));
    holder.subtitle.setText(post.get("subtitle"));
    return vi;
}

holder 只是一个简单的类:

private class ViewHolder {
    public TextView title;
    public TextView subtitle;
}

我已经做到了,我还在所有可绘制文件夹中复制了 xml 可绘制文件(全部合并)。因此,其中一项操作修复了它,但我不太确定是什么..

于 2013-12-04T05:36:52.987 回答
1

Try passing in the parent when you inflate your cell. Using this method:

public View inflate (int resource, ViewGroup root, boolean attachToRoot)

vi = inflater.inflate(R.layout.list_row, parent, false);
于 2013-12-03T06:02:22.340 回答
0

如果您收到 android.widget.AbsListView.obtainView(AbsListView.java:1408) 异常,则返回的 getView() 视图可能为 null。所以首先检查您是否没有返回 null 视图。

于 2013-12-03T06:29:31.550 回答