2012-04-20 20 views
59

Wprowadzam widok listy rozwijanej w systemie Android i otrzymuję powyższy błąd. Proszę pomóż mi.ExpandableListView -UnsupportedOperationException: addView (View, LayoutParams) nie jest obsługiwany w AdapterView

Główna działalność jest -

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); 
    } 


} 

klasa ExpAdapter jest - I wprowadziły adapter w drugiej klasie i nazwali go mt głównej działalności

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-

to jest xnl, który jest wyświetlany jako pierwszy przez główną działalność

<?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

to XML elementów grupy

<?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 to XML elementów podrzędnych

<?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

Wystąpił błąd, ponieważ ** 'dodano pusty widok w niewłaściwym miejscu' **. Powinieneś ** 'not' ** umieścić' pusty widok' pomiędzy tagami początkowym i zamykającym 'ExpandableListView'.Umieść go po zamknięciu tagu 'ExpandableListView'. –

Odpowiedz

127

Wydaje się, że nie Adapterview nie zezwalaj na dodawanie nowego widoku, Napotkałem ten sam problem

Rozwiąż go zastępując następującą linię

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

do

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

Zamiast nie używać w ogóle rodzica, powinieneś po prostu powiedzieć Inflaterowi, aby nie przywiązywał nadmuchanego widoku do rodzica za pomocą 'convertView = inflator.inflate (R.layout.child_rows, parent, false);'. Zobacz także [tę odpowiedź] (http://stackoverflow.com/a/6419586/484293). – blubb

+1

Kiedy jest powód, aby ustawić to na true? – user1007522

+3

Zwykle sposób, w jaki inflator działa, polega na podaniu odniesienia do rodzica. W takim przypadku zwracany obiekt będzie obiektem nadrzędnym z dodanym potomkiem, a nie dodanym obiektem widoku podrzędnego. Zwykle jest to używane, gdy chcesz tylko dodać widoki do obiektu grupy przeglądania, takiego jak linearlayout. –

11

pamiętać, że można również uzyskać ten błąd, gdy xml układ jest nieprawidłowy.

0

Zgodnie z Androidem Lint, Twój widok dziecka powinien zawsze zawierać odniesienie do jego widoku rodzica po zawyżeniu. Miałem dokładnie ten sam błąd w moim kodzie. Wystąpił, ponieważ TextView został umieszczony wewnątrz ExpandableListView. Kiedy przestawiłem mój układ xml, błąd przestał się pojawiać.

0

Ten błąd może być również spowodowany przez funkcję natychmiastowego uruchomienia. Pracowałem nad listview iz powodu tego błędu aplikacja ciągle się zawiesza. Odinstalowanie aplikacji i ponowne uruchomienie rozwiązało problem.

2

Jak stwierdzono powyżej,

Zamiast nieużywania rodziców w ogóle, należy po prostu powiedzieć Inflater nie dołączyć zawyżone widok do rodzica z

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

Zobacz także ten answer.

Powodem jest to, że adapter zajmuje się dołączaniem widoków do rodzica.

Powiązane problemy