Android listview布局返回空视图

Android listview布局返回空视图,android,android-layout,android-activity,android-listview,android-adapter,Android,Android Layout,Android Activity,Android Listview,Android Adapter,我正在尝试创建一个具有多个布局的列表视图。布局由数字0-5拾取。所以我有6种可能的布局。我使用一个开关盒来膨胀与数字对应的布局。我有一些问题。首先,我得到了某些布局的副本,这些布局具有以前布局的值,甚至不应该存在。下面代码的另一个问题是,如果我滚动到buttom,最后一个元素的布局是案例4,那么当我滚动到顶部并返回到buttom时,它的布局是不同案例的。为什么会发生这种情况。另外,如果我尝试使用settext()设置textview,我会得到一个错误,说textview为null @Overri

我正在尝试创建一个具有多个布局的列表视图。布局由数字0-5拾取。所以我有6种可能的布局。我使用一个开关盒来膨胀与数字对应的布局。我有一些问题。首先,我得到了某些布局的副本,这些布局具有以前布局的值,甚至不应该存在。下面代码的另一个问题是,如果我滚动到buttom,最后一个元素的布局是案例4,那么当我滚动到顶部并返回到buttom时,它的布局是不同案例的。为什么会发生这种情况。另外,如果我尝试使用settext()设置textview,我会得到一个错误,说textview为null

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder1 = null;
    View v = convertView;

    q = getItemViewType(getType(type, position));

    if (v == null) {
        holder1 = new ViewHolder();

        switch (q) {
            case 0:
                v = LayoutInflater.from(context).inflate(R.layout.layout0, parent, false);
                holder1.mainpic = (ImageView) v.findViewById(R.id.mainimage0);
                holder1.string = (TextView) v.findViewById(R.id.string0);
                holder1.string2 = (TextView) v.findViewById(R.id.string_two0);
                break;
            case 1:
                v = LayoutInflater.from(context).inflate(R.layout.layout1, parent, false);
                holder1.mainpic = (ImageView) v.findViewById(R.id.mainimage1);
                holder1.string = (TextView) v.findViewById(R.id.string1);
                break;
            case 2:
                v = LayoutInflater.from(context).inflate(R.layout.layout2, parent, false);
                holder1.mainpic = (ImageView) v.findViewById(R.id.mainimage2);
                holder1.string = (TextView) v.findViewById(R.id.string2);
                break;
            case 3:
                v = LayoutInflater.from(context).inflate(R.layout.layout3, parent, false);
                holder1.mainpic = (ImageView) v.findViewById(R.id.mainimage3);
                holder1.string = (TextView) v.findViewById(R.id.string3);
                break;
            case 4:
                v = LayoutInflater.from(context).inflate(R.layout.layout4, parent, false);
                holder1.mainpic = (ImageView) v.findViewById(R.id.mainimage4);
                holder1.string = (TextView) v.findViewById(R.id.string4);
                break;
            default:
                v = LayoutInflater.from(context).inflate(R.layout.layout5, parent, false);
                holder1.mainpic = (ImageView) v.findViewById(R.id.mainimage5);
                holder1.string = (TextView) v.findViewById(R.id.string5);
                break;
        }

        v.setTag(holder1);
    } else {
        holder1 = (ViewHolder) v.getTag();
    }

    ///holder1.string.settext(" some text")   error

    return v;
}











@Override
public int getItemViewType(int value) {

int type;
if(value ==0)
{
    type=0;
}

else if(value ==1)
{
    type=1;
}else if(value ==2)
{
    type=2;
}
else if(value ==3)
{
    type=3;
}
else if(value ==4)
{
    type=4;
}else
{
    type=5;
}



return type;

}

清理代码后,很明显您有一些错误代码将
字符串
视图(视图的糟糕名称,顺便说一句)分配了两次:

holder1.string = (TextView) v.findViewById(R.id.string0);
holder1.string = (TextView) v.findViewById(R.id.string_two0);

其中一个可能是错误的

你应该修改一下你的代码。所有的空格和异常缩进都有点难以阅读Demagic好的,我清理了它一个位检查,以确保getCount()方法返回项目数。不,这不是问题。在我把代码放到网上之前,我不得不稍微修改一下。这就是我的名字不好的原因嗯,我可能还建议您发布getItemViewType()和getViewTypeCount()的代码。并仔细检查您的所有布局是否都有您要查找的视图ID。@kcopock-我已经检查了我的布局,以确保所有ID都在那里。我发布了我的GetItemViewType在您的开关案例中的每个
holder.string
赋值之后添加一些日志记录——查看在
findViewById()
调用之后是否有任何赋值为null。@kCoPock-我尝试了一些日志记录“如果(holder.string==null)log.d(“,“null”)”视图不为null。