问题描述
我有要加载到 ExpandableListView 的对象的 ArrayList。 标题已正确加载和显示,但是当我想扩展标题之一时,我得到了期望
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
并且此异常发生在方法 getChildrenCount(int groupPosition)
当我在返回时设置断点时,我看到 _listDataChild 和 _listDataHeader 都有数据并且一切都已初始化,所以我无法弄清楚为什么会出现空指针异常。
代码如下:
public class ExpandableAdapter extends Baseexpandablelistadapter {
private Context _context;
private ArrayList<SPCLogModel> _listDataHeader = new ArrayList<SPCLogModel>();
private HashMap<ArrayList<SPCLogModel>,ArrayList<SPCLogModel>> _listDataChild = new HashMap<ArrayList<SPCLogModel>,ArrayList<SPCLogModel>>();
public ExandebleAdapter(Context _context,ArrayList<SPCLogModel> listDataHeader,HashMap<ArrayList<SPCLogModel>,ArrayList<SPCLogModel>> listDataChild) {
this._context = _context;
this._listDataHeader = listDataHeader;
this._listDataChild = listDataChild;
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@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 Object getChild(int groupPosition,int childPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition,int childPosition) {
return childPosition;
}
@Override
public boolean hasstableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition,boolean isExpanded,View convertView,ViewGroup parent) {
//String headerTitle = (String) getGroup(groupPosition);
SPCLogModel mod = (SPCLogModel) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_group,null);
}
TextView listTitle = (TextView) convertView
.findViewById(R.id.tv_listtitle);
listTitle.setTypeface(null,Typeface.BOLD);
//listTitle.setText(headerTitle);
listTitle.setText(mod.getDO_No());
return convertView;
}
@Override
public View getChildView(int groupPosition,final int childPosition,boolean isLastChild,ViewGroup parent) {
//final String childText = (String) getChild(groupPosition,childPosition);
SPCLogModel mod = (SPCLogModel) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_child,null);
}
TextView expandedListTextView = (TextView) convertView
.findViewById(R.id.tv_listchild);
//expandedListTextView.setText(childText);
expandedListTextView.setText(mod.getbrojKutije().toString());
return convertView;
}
@Override
public boolean isChildSelectable(int i,int i1) {
return false;
}
获取数据
public class asyncTaskGetAllSPC extends AsyncTask<String,String,ArrayList<SPCLogModel>> {
// For task finish detection
public interface AsynResponse {
void processFinish(Boolean output);
void processFinish(Boolean output,ArrayList<SPCLogModel> listspc,ArrayList<SPCLogModel> listspcgen);
}
AsynResponse asynResponse = null;
final ArrayList<SPCLogModel> itemListSPC = new ArrayList<SPCLogModel>();
final ArrayList<SPCLogModel> itemListProvjeraGen = new ArrayList<SPCLogModel>();
private Fragment activityName;
public asyncTaskGetAllSPC() {
super();
}
public asyncTaskGetAllSPC(galleryFragment activityName) {
this.activityName = activityName;
}
// For Feedback info if task is finished
public asyncTaskGetAllSPC(Fragment activityName,AsynResponse asynResponse) {
this.activityName = activityName;
this.asynResponse = asynResponse;
}
@Override
protected ArrayList<SPCLogModel> doInBackground(String... strings) {
//return null;
// return new ProcessData().ProcessOrders();
String retVal = new ProcessData().ProcessGetRequest(Api.URL_GET_ALLSPC);
ArrayList<HashMap<String,String>> listaStavki = new ArrayList<HashMap<String,String>>();
listaStavki = new ProcessData().DecodeSPCAll(retVal);
for (int i=0; i<=listaStavki.size()-1; i++) {
SPCLogModel spclogheader = new SPCLogModel(
listaStavki.get(i).get("UID"),listaStavki.get(i).get("DatumSPC"),Integer.parseInt(listaStavki.get(i).get("IDKontrolor")),listaStavki.get(i).get("DO_No"),listaStavki.get(i).get("LI_Article"),Integer.parseInt(listaStavki.get(i).get("ProducedQTY"))
);
SPCLogModel spcprovjeragen = new SPCLogModel(
listaStavki.get(i).get("UIDOrder"),Integer.parseInt(listaStavki.get(i).get("brojKutije")),Integer.parseInt(listaStavki.get(i).get("brojKomada")),listaStavki.get(i).get("Napomena")
);
itemListSPC.add(spclogheader);
itemListProvjeraGen.add(spcprovjeragen);
}
HashSet<String> seen=new HashSet<>();
final boolean b = itemListSPC.removeIf(e -> !seen.add(e.getUID()));
return itemListSPC;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(ArrayList<SPCLogModel> items) {
super.onPostExecute(items);
asynResponse.processFinish(true,itemListSPC,itemListProvjeraGen);
}
填充 ExpandableListView
private void LoadData()
{
asyncTaskGetAllSPC t = (asyncTaskGetAllSPC) new asyncTaskGetAllSPC(ListSPC.this,new asyncTaskGetAllSPC.AsynResponse() {
@Override
public void processFinish(Boolean output) {
}
@Override
public void processFinish(Boolean output,ArrayList<SPCLogModel> listspcgen) {
ArrayList<SPCLogModel> itemListSPC = listspc;
ArrayList<SPCLogModel> itemListProvjeraGen = listspcgen;
listDataChild = new HashMap<ArrayList<SPCLogModel>,ArrayList<SPCLogModel>>();
listDataHeader = listspc;
listDataChild.put(listDataHeader,listspcgen);
PopulateExList();
}
}).execute();
}
private void PopulateExList()
{
expandablelistadapter = new ExandebleAdapter(ListSPC.this.getActivity(),listDataHeader,listDataChild);
exp_leaSEOffer.setAdapter(expandablelistadapter);
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)