我首先在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 ;
}
先謝謝了!
請發佈logcat –
我發佈了@Shayanpourvatan – mismanc
表示「ExpandListAdapter」的第105行 –