ImageView在android的自定义ArrayList适配器中保持NULL



我试图在自定义ArrayAdapter中达到我的ImageView和TextView。但问题是,android找不到它,它是空的,我不能使用它。

当我试图到达它时,它抛出一个空异常:(imageview.set…)。

这是我的arrayadapter的类代码:

    package ***.***.***;
import android.app.Activity;
import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
 * Created by yoav on 09/08/15.
 */
public class ItemAdapter extends ArrayAdapter<Item> {
    // declaring our ArrayList of items
    private ArrayList<Item> cards;
    Context mContext;
    /* here we must override the constructor for ArrayAdapter
    * the only variable we care about now is ArrayList<Item> objects,
    * because it is the list of objects we want to display.
    */
    public ItemAdapter(Context context, int textViewResourceId, int textV, ArrayList<Item> newCards) {
        super(context, textV, textViewResourceId, newCards);
        this.cards = newCards;
        mContext = context;
    }
    /*
     * we are overriding the getView method here - this is what defines how each
     * list item will look.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View v = convertView;
        ViewHolder holder = new ViewHolder();
        // First let's verify the convertView is not null
        if (convertView == null) {
            // This a new view we inflate the new layout
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.item, parent, false);
            // Now we can fill the layout with the right values
            TextView tv = (TextView) v.findViewById(R.id.tNameee);
            ImageView img = (ImageView) v.findViewById(R.id.ivUserImage);
            holder.txtTitle = tv;
            holder.imageView = img;

            v.setTag(holder);
        }
        else
            holder = (ViewHolder) v.getTag();
        System.out.println("Position [" + position + "]");
        Item p = cards.get(position);
        holder.txtTitle.setText(p.getName());
        holder.imageView.setImageResource(R.drawable.******);

        return v;
    }
    private class ViewHolder {
        ImageView imageView;
        TextView txtTitle;
    }
}

我的XML文件item.xml:

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_gravity="bottom|center_horizontal"
    android:layout_marginBottom="15dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/tNameee"
        android:textSize="40sp"
        android:textColor="@android:color/white"
        android:background="@drawable/polaroid"
        android:gravity="center"
        tools:text="@string/hello_world"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <ImageView
        android:layout_width="282dp"
        android:layout_height="255dp"
        android:layout_margin="10dp"
        android:background="#1234ff"
        android:id="@+id/ivUserImage"/>
    <View
        android:id="@+id/item_swipe_left_indicator"
        android:alpha="0"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_margin="10dp"
        android:background="@drawable/polaroid" />
    <View
        android:id="@+id/item_swipe_right_indicator"
        android:alpha="0"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_margin="10dp"
        android:layout_gravity="right"
        android:background="@drawable/polaroid" />
</FrameLayout>
有人知道我做错了什么吗?

item.xml中有一个名为"textteeee"的对象。还有一个对象叫"ivUserImage.xml"

这可能不会有帮助,但尝试直接分配视图:

@Override
public View getView(int position, View convertView, ViewGroup parent){
    View v = convertView;
    ViewHolder holder = new ViewHolder();
    // First let's verify the convertView is not null
    if (convertView == null) {
        // This a new view we inflate the new layout
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.item, parent, false);
        // Now we can fill the layout with the right values
        holder.tv = (TextView) v.findViewById(R.id.tNameee);
        holder.img = (ImageView) v.findViewById(R.id.ivUserImage);
        v.setTag(holder);
    }
    else
        holder = (ViewHolder) v.getTag();
    System.out.println("Position [" + position + "]");
    Item p = cards.get(position);
    holder.tv.setText(p.getName());
    holder.img.setImageResource(R.drawable.******);

    return v;
}
private class ViewHolder {
    ImageView tv;
    TextView img;
}

如果你需要更多的帮助,请张贴xml文件

最新更新