我是 Android应用程序开发的初学者。我对Java`集合没有很好的了解。我必须在可扩展列表中显示数据。我们正在使用 OData服务从后端获取数据。
我已按照http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/在可展开列表中显示我的数据。我能够显示标题数据。但是,当我在可扩展列表视图中单击Header项时,我的Adapter TeamDetailsAdapter中的getChildrenCount()
处的空指针异常。
我的活动如下。我没有提到整个代码。我在下面提到了fetchData()
方法。
private ArrayList<Level1> listDataHeader = new ArrayList<Level1>();
private HashMap<String, ArrayList<Level2>> listDataChild = new HashMap<String, ArrayList<Level2>>();
private ArrayList<Level2> emps = new ArrayList<Level2>();
fetchData();
expListView = (ExpandableListView) findViewById(R.id.lvTeamDetailsExpList);
expListAdapter = new TeamDetailsAdapter(this, listDataHeader, listDataChild);
expListView.setAdapter(expListAdapter);
fetchdata()
{
.................
................
for(int i =0; i<n; i++)
{
ISDMODataEntry entry = entries.get(i);
if(entry.getPropertyValue("Olevel").equals("1"))
{
Level1 _level1 = new Level1();
_level1.setEmpId(entry.getPropertyValue("EmpNo"));
_level1.setEmpName(entry.getPropertyValue("EmpName"));
listDataHeader.add(_level1);
}
}
for(int j = 0; j<listDataHeader.size(); j++)
{
for(int k =0; k<n; k++)
{
ISDMODataEntry entry2 = entries.get(k);
Level2 _level2 = new Level2();
if(entry2.getPropertyValue("ManagerName").equals(listDataHeader.get(j).getEmpName()))
{
System.out.println("MngrName: " + entry2.getPropertyValue("ManagerName") + "Row name: " + listDataHeader.get(j).getEmpName());
_level2.setEmpId(entry2.getPropertyValue("EmpNo"));
_level2.setEmpName(entry2.getPropertyValue("EmpName"));
emps.add(_level2);
listDataChild.put(listDataHeader.get(j).getEmpName().toString(), emps);
}
}
System.out.println("Emp Size: " + emps.size());
}
}
以下是我的适配器类
public class TeamDetailsAdapter extends BaseExpandableListAdapter{
private Context _context;
private ArrayList<Level1> _listDataHeader;
private HashMap<String, ArrayList<Level2>> _listDataChild;
public TeamDetailsAdapter(Context context,
ArrayList<Level1> listDataHeader,
HashMap<String, ArrayList<Level2>> listDataChild) {
// TODO Auto-generated constructor stub
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listDataChild;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
//final String childText = (String) getChild(groupPosition, childPosition);
final Level2 childText = (Level2) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(
R.layout.team_details_list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText.getEmpName());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
//String headerTitle = (String) getGroup(groupPosition);
Level1 headeTitle = (Level1) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(
R.layout.team_details_list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
//lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headeTitle.getEmpName());
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Level1 Model class
public class Level1 {
private String empId = "";
private String empName = "";
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
}
2级模型类
public class Level2 {
private String empId= "";
private String empName = "";
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
}
请帮帮我。我陷入了困境。
被修改
以下是日志猫:
下面是logcat:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.XXXXXXXX.XXXXXXXX.TeamDetailsAdapter.getChildrenCount(TeamDetailsAdapter.java:78)
at android.widget.ExpandableListConnector.refreshExpGroupMetadataList(ExpandableListConnector.java:561)
at android.widget.ExpandableListConnector.expandGroup(ExpandableListConnector.java:682)
at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:567)
at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:527)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)