Unable to start activity:UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

前端 未结 4 1389
南方客
南方客 2020-11-30 04:21

I want to write a ListView in basic format but I get an error:

UnsupportedOperationException: addView(View, LayoutParams) is not supported in Ad         


        
相关标签:
4条回答
  • 2020-11-30 05:03

    what should i do???

    Correct your code.

    UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

    A subclass of AdapterView like a ListView can't have children manually added either in the layout file or added in code. So if you have this in one of your layouts:

    <ListView // .. other attributes>
         <// other views <-- notice the children of the ListView tag
    </ListView>
    

    don't do it, as this will call the addView method of ListView, throwing the exception. Instead use:

    <ListView // .. other attributes />
    < // other views
    

    You also can't use any of the addView methods of ListView in code like this:

    listViewReference.addView(anotherView); // <-- don't do it
    

    Also, if you use the LayoutInflater.inflate method in the code of the Activity or the adapter(its getView method), don't pass the ListView as the second parameter. For example, don't use:

    convertView  = inflator.inflate(R.layout.child_rows, parent);
    

    as in Tamilarasi Sivaraj's answer as that will throw the exception again. Instead use:

    convertView  = inflator.inflate(R.layout.child_rows, parent, false);
    

    Related to the exception you posted in the question, it appears you use the setText method with an int(one of the s or i arrays being an int array). The problem is that in this case TextView will think you're trying to set the text using a string resource like this R.string.astring. The int you pass is not a string resource so an exception will be thrown. If s or i is an int and you're trying to show it in the TextView use this instead:

    tv.setText(String.valueOf(s[position])); // assuming s is the int array
    
    0 讨论(0)
  • 2020-11-30 05:04

    Try this Like this:

    public static class ViewHolder {
        public ImageView imageView;
        public TextView textView; 
    
        public View getView(int position, View convertView, ViewGroup parent) {
            try{
                ViewHolder holder;
    
                if (convertView == null)  {
                    holder = new ViewHolder(); 
                    LayoutInflater inflater = context1.getLayoutInflater();
                    convertView = inflater.inflate(R.layout.your_layout, null,true);  
    
                    holder.imageView = (ImageView) convertView.findViewById(R.id.imgview);
    
                    holder.textView = (TextView) convertView.findViewById(R.id.txtview);
    
                    convertView.setTag(holder);  
                } else {
                    holder = (ViewHolder) convertView.getTag();  
                }
    
                holder.textView.setText("your text");
                holder.imageView.setImageResource("your image resource");
            } catch (Exception e) {
                System.out.println("Error: "+e);    
            }
    
            return convertView;
        }
    }
    
    0 讨论(0)
  • 2020-11-30 05:16

    Replace your layout in inflater

     @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
    
        if (convertView == null) {
    
            LayoutInflater inflater = (LayoutInflater) LayoutInflater
                    .from(contex);
            convertView = inflater.inflate(R.layout.your_layout, parent, false);
    
        }
    
        txtName = (TextView) convertView.findViewById(R.id.txtName);
        txtName.setText(""+ContactsArr.get(position).get("ContName"));
        txtPhoneNumber = (TextView) convertView.findViewById(R.id.txtContact);
        txtPhoneNumber.setText(""+ContactsArr.get(position).get("ContPhone"));
    
        return convertView;
    }
    
    0 讨论(0)
  • 2020-11-30 05:17

    If you're like me and totally skipped over the answer I needed in @Luksprog's answer because it's buried in it, try using adding the third parameter to the inflate method, also mentioned here:

    inflater.inflate(R.layout.group_row, parent, false);
    

    Don't use true instead of false or you will get the same error that brought you here, because it's trying to attach/add it to the parent, hence the addView is not supported in AdapterView error.

    0 讨论(0)
提交回复
热议问题