2012-04-20 7 views
59

Ich bin Expandable List-Ansicht in Android implementieren und ich bekomme den oben genannten Fehler. Bitte hilf mir.ExpandableListView -UnsupportedOperationException: AddView (Ansicht, LayoutParams) wird nicht in AdapterView unterstützt

Haupttätigkeit ist -

package com.expand; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.DisplayMetrics; 
import android.util.Log; 
import android.view.View; 
import android.widget.ExpandableListView; 
import android.widget.Toast; 


public class MyExpandableListViewActivity extends Activity { 
    /** Called when the activity is first created. */ 



    static final String groupElements[]= { 
      "India", 
      "Australia", 
      "England", 
      "South Africa" 
     }; 

    static final String[][] childElements= 
    { 
      { 
      "Sachin Tendulkar", 
      "Raina", 
      "Dhoni", 
      "Yuvi" 
      }, 
      { 
      "Ponting", 
      "Adam Gilchrist", 
      "Michael Clarke" 
      }, 
      { 
      "Andrew Strauss", 
      "kevin Peterson", 
      "Nasser Hussain" 
      }, 
      { 
      "Graeme Smith", 
      "AB de villiers", 
      "Jacques Kallis" 
      } 
      }; 



    DisplayMetrics metrics; 
    int width; 
    ExpandableListView expandList; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     expandList = (ExpandableListView)findViewById(R.id.expandList1); 
     metrics = new DisplayMetrics(); 

     getWindowManager().getDefaultDisplay().getMetrics(metrics); 
     width = metrics.widthPixels; 

     //ExpAdapter adapter = new ExpAdapter(MyExpandableListViewActivity.this, groupElements, childElements); 

     expandList.setAdapter(new ExpAdapter(MyExpandableListViewActivity.this, groupElements, childElements)); 
     expandList.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10)); 


     expandList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 

      @Override 
      public void onGroupExpand(int groupPosition) { 
       // TODO Auto-generated method stub 

       Log.e("onGroupExpand", "OK"); 
      } 
     }); 

     expandList.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { 

      @Override 
      public void onGroupCollapse(int groupPosition) { 
       // TODO Auto-generated method stub 

       Log.e("onGroupCollapse", "OK"); 

      } 
     }); 

     expandList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 



      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, 
        int groupPosition, int childPosition, long id) { 

       //getting the item that is selected 
       //String s = (String) expandList.getItemAtPosition((int) id); 

       Toast.makeText(MyExpandableListViewActivity.this, "You have selected :" , Toast.LENGTH_SHORT).show(); 

       return false; 
      } 
     }); 

    } 



    public int GetDipsFromPixel(float pixels) 
    { 
     // Get the screen's density scale 
     final float scale = getResources().getDisplayMetrics().density; 
     // Convert the dps to pixels, based on density scale 
     return (int) (pixels * scale + 0.5f); 
    } 


} 

ExpAdapter Klasse ist - ich den Adapter in anderen Klasse implementiert haben und es in mt Haupttätigkeit genannt

package com.expand; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseExpandableListAdapter; 
import android.widget.TextView; 



    public class ExpAdapter extends BaseExpandableListAdapter { 

     public Context myContext; 
     public String[][] childElements; 
     TextView childValues; 
     public String[] groupElements; 


     public ExpAdapter(Context context, String[] group, String[][] childs) 
     { 

      this.myContext=context; 
      this.groupElements = group; 
      this.childElements = childs; 

     } 



     @Override 
     public Object getChild(int groupPosition, int childPosition) { 
      // TODO Auto-generated method stub 
      return childElements[groupPosition][childPosition]; 
     } 

     @Override 
     public long getChildId(int groupPosition, int childPosition) { 
      // TODO Auto-generated method stub 

      return 0; 
     } 

     @Override 
     public View getChildView(int groupPosition, int childPosition, 
       boolean isLastChild, View convertView, ViewGroup parent) { 
      // TODO Auto-generated method stub 

      if(convertView==null){ 

       LayoutInflater inflator = (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = inflator.inflate(R.layout.child_rows, parent); 

      } 
      childValues = (TextView)convertView.findViewById(R.id.rowValues); 
      childValues.setText(childElements[groupPosition][childPosition]); 

      return convertView; 
     } 

     @Override 
     public int getChildrenCount(int groupPosition) { 
      // TODO Auto-generated method stub 
      return groupElements[groupPosition].length(); 
     } 

     @Override 
     public Object getGroup(int groupPosition) { 
      // TODO Auto-generated method stub 
      return groupElements[groupPosition]; 
     } 

     @Override 
     public int getGroupCount() { 
      // TODO Auto-generated method stub 
      return groupElements.length; 
     } 

     @Override 
     public long getGroupId(int groupPosition) { 
      // TODO Auto-generated method stub 
      return 0; 
     } 

     @Override 
     public View getGroupView(int groupPosition, boolean isExpanded, 
       View convertView, ViewGroup parent) { 
      // TODO Auto-generated method stub 

      if(convertView==null){ 
       LayoutInflater inflator = (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = inflator.inflate(R.layout.group_rows, null); 
      } 
      TextView group = (TextView)convertView.findViewById(R.id.groupId); 
      group.setText(groupElements[groupPosition]); 

      return convertView; 
     } 

     @Override 
     public boolean hasStableIds() { 
      // TODO Auto-generated method stub 
      return false; 
     } 

     @Override 
     public boolean isChildSelectable(int groupPosition, int childPosition) { 
      // TODO Auto-generated method stub 
      return true; 
     } 




    } 

main.xml-

Dies ist das xnl, das bei der ersten Aktivität

angezeigt wird
<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 



     <ExpandableListView 
      android:id="@+id/expandList1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      > 


       <TextView 
       android:id="@+id/android:empty" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       > 
      </TextView> 


     </ExpandableListView> 


    </LinearLayout> 

group_row.xml

ist dies die xml für die Gruppenelemente

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/gropu_name" 
     android:layout_width="fill_parent" 
     android:layout_height="40dp" 
     android:orientation="vertical" > 


     <TextView 
      android:id="@+id/groupId" 
      android:layout_height="40dp" 
      android:layout_width="wrap_content" 
      android:paddingLeft="30dp" 
      android:gravity="center_vertical" 
      android:textSize="16dp" 
      android:textStyle="bold" 
      /> 

    </LinearLayout> 

child_row.xml dies die XML für die untergeordneten Elemente ist

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="40dp" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/rowValues" 
     android:layout_width="wrap_content" 
     android:layout_height="30dp" 
     android:gravity="center_vertical" 
     android:paddingLeft="50dp" 
     android:textSize="12dp" /> 


</LinearLayout> 
+0

Fehler tritt auf, weil ** 'Sie leere Ansicht an falscher Stelle hinzugefügt haben **. Sie sollten "nicht" ** eine leere Ansicht zwischen den Start- und End-Tags von "ExpandableListView" einfügen.Setzen Sie es nach dem Schließen des Tags von 'ExpandableListView'. –

Antwort

127

Scheint, wie Adapterview tut nicht zulassen, neue Ansicht hinzuzufügen, Ich stieß auf das gleiche Problem

es lösen, indem folgende Zeile

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

zu

convertView = inflator.inflate(R.layout.child_rows, null); 
+116

Anstatt überhaupt kein Elternteil zu verwenden, sollten Sie dem Inflater einfach sagen, dass er die aufgeblähte Ansicht nicht an das Elternobjekt mit 'convertView = inflator.inflate (R.layout.child_rows, parent, false) anhängen soll;'. Siehe auch [diese Antwort] (http://stackoverflow.com/a/6419586/484293). – blubb

+1

Wann gibt es einen Grund, dies auf wahr zu setzen? – user1007522

+3

Normalerweise funktioniert der Inflator, wenn Sie eine Elternreferenz angeben, diese Ansicht als Elternreferenz hinzufügen. In diesem Fall ist das zurückgegebene Objekt ein übergeordnetes Objekt, dem ein untergeordnetes Objekt hinzugefügt wurde, und nicht das von Ihnen hinzugefügte untergeordnete Ansichtsobjekt. Normalerweise wird dies verwendet, wenn Sie nur Ansichten zu einem Ansichtsgruppenobjekt wie linearlayout hinzufügen möchten. –

11

Hinweis ersetzt, die Sie auch diesen Fehler, wenn ungültig Ihr Layout XML ist bekommen.

0

Laut Android Lint sollte Ihre Kinderansicht immer einen Verweis auf die Elternansicht anzeigen, wenn sie aufgeblasen ist. Ich hatte genau den gleichen Fehler in meinem Code. Es trat auf, weil TextView in ExpandableListView platziert wurde. Als ich mein XML-Layout neu anordnete, wurde der Fehler nicht mehr angezeigt.

0

Dieser Fehler kann auch durch sofortige Ausführung Funktion verursacht werden. Ich arbeitete an Listview und wegen dieser Fehler App stürzte ständig ab. Durch das Deinstallieren der App und das erneute Ausführen wurde der Fehler behoben.

2

Wie oben erwähnt wurden,

Statt kein Elternteil überhaupt verwenden, sollten Sie einfach sagen, die Inflater nicht den aufgeblasenen Blick auf die Eltern befestigen mit

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

Siehe auch diese answer.

Der Grund dafür ist, dass der Adapter das Anhängen von Ansichten an die übergeordneten Elemente übernimmt.