java.lang.IllegalArgumentException:如果不能有一个viewTypeCount< 1 [英] java.lang.IllegalArgumentException: Can't have a viewTypeCount < 1

查看:1532
本文介绍了java.lang.IllegalArgumentException:如果不能有一个viewTypeCount< 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个错误:

  java.lang.IllegalArgumentException:如果不能有一个viewTypeCount< 1
 

我是pretty的肯定,我知道到底是什么原因引起的,但我不知道如何解决它。

我的应用程序加载从数据库中的用户朋友。当用户至少有1位朋友把在列表视图中,它的罚款。当用户是全新的,没有朋友,应用程序崩溃,因为列表视图为0计数。

时的错误处理这只是一个个案?

如果我不发布所有必要的相关code,请让我知道!

下面是我的适配器:

 公共类MyAdapter扩展ArrayAdapter< HashMap的<字符串,字符串>> {

    上下文语境;
    INT RESOURCEID;
    LayoutInflater充气;
    私人语境mContext;

    @覆盖

    公众诠释getViewTypeCount(){

        返回getCount将();
    }

    @覆盖
    公众诠释getItemViewType(INT位置){

        返回的位置;
    }


    ArrayList的< HashMap的<字符串,字符串>>项目;
    公共MyAdapter(上下文的背景下,INT RESOURCEID,ArrayList的< HashMap的<字符串,字符串>>项目)
    {
        超(背景下,RESOURCEID,项目);
        mContext =背景;
        this.items =项目;
        充气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        最后ViewHolder持有人;
        如果(convertView == NULL){

            convertView = inflater.inflate(R.layout.list_item,NULL);

            持有人=新ViewHolder();
            holder.fbphoto =(ImageView的)convertView.findViewById(R.id.fbphoto);
            holder.name =(TextView中)convertView.findViewById(R.id.name);

   convertView.setTag(保持器);

        } 其他 {

            支架=(ViewHolder)convertView.getTag();
        }



        最后的HashMap<字符串,字符串>项目=(HashMap的<字符串,字符串>)items.get(位置);
        如果(项目!= NULL)
        {

            字符串facebookProfilePicUrl =htt​​ps://graph.facebook.com/"+item.get(TAG_FACEBOOKID)+"/picture?width=150&height=150;

            Picasso.with(mContext)
            .load(facebookProfilePicUrl)
            .placeholder(R.drawable.no_image)
            。走进(holder.fbphoto);

holder.name.setText(item.get(TAG_USERNAME));

 }

        返回convertView;
    }

    公共类ViewHolder
    {
        ImageView的fbphoto;
        TextView的名称;

    }
}
 

解决方案

我想你好想ViewTypeCount点。你应该返回列表的不同视图类型的数量。 这是循环列表里的意见很重要。

成像有2种listItems中,其中一个的白色背景和一个黑色背景。当您返回2作为ViewTypeCount的列表视图知道好​​了,有2种listItems中,并且不会将它们混合起来,在getView视图循环。

所以只需使用:

 公众诠释getViewTypeCount(){
        返回1;
    }
 

或不覆盖的方法都没有。

I'm getting this error:

java.lang.IllegalArgumentException: Can't have a viewTypeCount < 1

I'm pretty sure I know exactly what's causing it, but I don't know how to fix it.

My app loads a users friends from the database. When the user has at least 1 friend to put in the list view, it's fine. When the user is brand new and has no friends yet, the app crashes because the listview has a count of 0.

Is this simply a case of error handling?

If I don't post all the necessary relevant code please let me know!

Here is my adapter:

public class MyAdapter extends ArrayAdapter<HashMap<String, String>> {

    Context context;
    int resourceId;
    LayoutInflater inflater;
    private Context mContext;

    @Override

    public int getViewTypeCount() {                 

        return getCount();
    }

    @Override
    public int getItemViewType(int position) {

        return position;
    }


    ArrayList<HashMap<String, String>>  items;
    public MyAdapter (Context context, int resourceId, ArrayList<HashMap<String, String>> items)
    {
        super(context, resourceId, items);
        mContext = context;
        this.items =items;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        final ViewHolder holder;
        if (convertView == null){

            convertView = inflater.inflate(R.layout.list_item, null);

            holder = new ViewHolder();
            holder.fbphoto = (ImageView)convertView.findViewById(R.id.fbphoto);
            holder.name = (TextView)convertView.findViewById(R.id.name);

   convertView.setTag(holder);

        } else {

            holder = (ViewHolder)convertView.getTag();
        }



        final HashMap<String,String> item = (HashMap<String,String> ) items.get(position);
        if (item != null)
        {

            String facebookProfilePicUrl = "https://graph.facebook.com/"+item.get(TAG_FACEBOOKID)+"/picture?width=150&height=150";

            Picasso.with(mContext)
            .load(facebookProfilePicUrl)
            .placeholder(R.drawable.no_image)
            .into(holder.fbphoto);

holder.name.setText(item.get(TAG_USERNAME));

 }

        return convertView;
    }

    public class ViewHolder
    {
        ImageView fbphoto;
        TextView    name;

    }
}

解决方案

I think you miss the point of ViewTypeCount. You should return the number of Different View Types in your list. This is important for recycling of the Views inside the List.

Imaging you have 2 Types of Listitems, one with a white Background and one with black Background. When you return 2 as ViewTypeCount the Listview knows ok, there a 2 types of Listitems and will not mix them up in the getView view recycling.

so just use:

   public int getViewTypeCount() {                 
        return 1;
    }

or dont override that method at all.

这篇关于java.lang.IllegalArgumentException:如果不能有一个viewTypeCount&LT; 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆