0

我首先在Android中使用ExpandableListView視圖。我希望groupviews在ExpandableView中擁有不同的佈局或項目。孩子們包括旋轉器,textviews,edittexts。Expandablelistview適配器無法正常工作

我的問題是擴展組,正確的佈局不膨脹或代碼崩潰,因爲空指針異常大多是導致旋轉。一些碼樣本都低於

ExpandableListAdapter.java

@Override 
public View getChildView(int groupPosition, final int childPosition, 
     boolean isLastChild, View convertView, ViewGroup parent) { 

    View v = null; 
    int itemId = (int) getChildId(groupPosition, childPosition); 

    if(convertView==null){ 
     switch (itemId) { 
      case 6: convertView = li.inflate(R.layout.expand_akis,null); 
        v = akiskan(convertView); 
       break; 
      case 9: convertView = li.inflate(R.layout.expand_kapasite,null); 
       break; 
      case 11:convertView = li.inflate(R.layout.expand_kapasite2,null); 
       break; 
      case 12:convertView = li.inflate(R.layout.expand_evapohava,null); 
       break; 
      case 14:convertView = li.inflate(R.layout.expand_evapohava,null); 
       break; 
      case 15: 
       break; 
      case 18: 
       break; 
      default: 
       break; 
     } 

    } 
    switch (itemId) { 
    case 6: v = akiskan(convertView); 
     break; 
    case 9: v = kapasite(convertView, childPosition); 
     break; 
    case 11:v = kapasite(convertView, childPosition); 
     break; 
    case 12:v = convertView; 
     break; 
    case 14:v = convertView; 
     break; 
    case 15: 
     break; 
    case 18: 
     break; 
    default: 
     break; 
    } 

    return v; 
} 

private View kapasite(View convertView, int childPosition) { 

     if(childPosition == 0){ 

      EditText editKapasite = (EditText)convertView.findViewById(R.id.editNumKap); 

      Spinner spinKapa = (Spinner) convertView.findViewById(R.id.spinKapa); 
      ArrayAdapter<CharSequence> adapKap = ArrayAdapter.createFromResource(activity, R.array.kapasiteler, 
        R.layout.spinnertext); 
      adapKap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
      spinKapa.setAdapter(adapKap); 
     } 
     else if(childPosition == 1){ 
      EditText editYuzdePlus = (EditText)convertView.findViewById(R.id.editYuzdePlus); 
      EditText editYuzdeMin = (EditText)convertView.findViewById(R.id.editYuzdeMin); 
     } 

    return convertView; 
} 

private View akiskan(View convertView) { 

    Spinner spinAkiskan = (Spinner) convertView.findViewById(R.id.spinAkis); 
    ArrayAdapter<CharSequence> adapAkis = ArrayAdapter.createFromResource(activity, R.array.akiskanlar, 
      R.layout.spinnertext); 
    adapAkis.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinAkiskan.setAdapter(adapAkis); 

    return convertView; 
} 

getChildId()函數根據groupPosition和childPosition

main.xml中

<ExpandableListView 
    android:id="@+id/expandableList" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@+id/detailAraButton" 
    android:layout_alignParentTop="true" /> 

expand_akis.xml

生成唯一id
<EditText 
     android:layout_alignParentLeft="true" 
     android:id="@+id/editNumKap" 
     android:layout_width="100dp" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:layout_marginLeft="20dp" 
     android:inputType="numberSigned" /> 


    <Spinner 
     android:layout_toRightOf="@+id/editNumKap" 
     android:layout_marginLeft="20dp" 
     android:id="@+id/spinKapa" 
     android:layout_width="85sp" 
     android:layout_height="wrap_content" 
     /> 

expand_kapasite.xml

<EditText 
     android:layout_alignParentLeft="true" 
     android:id="@+id/editNumKap" 
     android:layout_width="100dp" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:layout_marginLeft="20dp" 
     android:inputType="numberSigned" /> 


    <Spinner 
     android:layout_toRightOf="@+id/editNumKap" 
     android:layout_marginLeft="20dp" 
     android:id="@+id/spinKapa" 
     android:layout_width="85sp" 
     android:layout_height="wrap_content" 
     /> 

logcat的

02-23 14:11:21.952: E/AndroidRuntime(16384): FATAL EXCEPTION: main 
02-23 14:11:21.952: E/AndroidRuntime(16384): java.lang.NullPointerException 
02-23 14:11:21.952: E/AndroidRuntime(16384): at com.buzcelik.tools.ExpandListAdapter.kapasite(ExpandListAdapter.java:105) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at com.buzcelik.tools.ExpandListAdapter.getChildView(ExpandListAdapter.java:76) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:450) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.widget.AbsListView.obtainView(AbsListView.java:2025) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.widget.ListView.makeAndAddView(ListView.java:1820) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.widget.ListView.fillDown(ListView.java:672) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.widget.ListView.fillSpecific(ListView.java:1378) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.widget.ListView.layoutChildren(ListView.java:1651) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.widget.AbsListView.onLayout(AbsListView.java:1876) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.view.View.layout(View.java:11425) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.view.ViewGroup.layout(ViewGroup.java:4232) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.view.View.layout(View.java:11425) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.view.ViewGroup.layout(ViewGroup.java:4232) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.widget.FrameLayout.onLayout(FrameLayout.java:431) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.view.View.layout(View.java:11425) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.view.ViewGroup.layout(ViewGroup.java:4232) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1589) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.view.View.layout(View.java:11425) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.view.ViewGroup.layout(ViewGroup.java:4232) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.widget.FrameLayout.onLayout(FrameLayout.java:431) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.view.View.layout(View.java:11425) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.view.ViewGroup.layout(ViewGroup.java:4232) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.view.View.layout(View.java:11425) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.view.ViewGroup.layout(ViewGroup.java:4232) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.widget.FrameLayout.onLayout(FrameLayout.java:431) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.view.View.layout(View.java:11425) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.view.ViewGroup.layout(ViewGroup.java:4232) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1509) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2498) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.os.Looper.loop(Looper.java:154) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at android.app.ActivityThread.main(ActivityThread.java:4944) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at java.lang.reflect.Method.invokeNative(Native Method) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at java.lang.reflect.Method.invoke(Method.java:511) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
02-23 14:11:21.952: E/AndroidRuntime(16384): at dalvik.system.NativeStart.main(Native Method) 

getChild()函數

@Override 
public long getChildId(int groupPosition, int childPosition) { 
    return 2*(childPosition+3+groupPosition)+groupPosition ; 
} 

先謝謝了!

+0

請發佈logcat –

+0

我發佈了@Shayanpourvatan – mismanc

+0

表示「ExpandListAdapter」的第105行 –

回答

2

問題是視圖回收。您將視圖設置爲空時一次。你的假設是,這些視圖將始終在將來調用該視圖時提供給getChildView。這是不正確的,一旦你摺疊了這些小孩的意見,現在有資格成爲另一個職位的子女。適配器回收它們。

有一種解決這個問題的簡單方法。簡單的方法是在每次調用getChildView時擴充視圖,以便覆蓋前一個視圖。我認爲你應該先嚐試一下,這樣你才能明白這是問題所在。

更復雜的方法是重寫getChildType()和getChildTypeCount()並強制前者爲每個您想要的佈局返回一個唯一的標識符(類似於您目前使用getChildId所做的),並使getChildTypeCount返回一個值等於唯一佈局的數量。

+0

好吧,我嘗試簡單的方式,它的工作正常,我會嘗試更復雜的方式:) – mismanc

+0

好吧,讓你重寫的函數在名字中有「type」。 – NameSpace

+0

getChildType()函數會返回什麼?它會保留默認的 – mismanc

相關問題