从BaseExpandableListAdapter中删除数据时为Nullpointer

时间:2015-09-02 14:09:58

标签: android nullpointerexception adapter expandablelistview notifydatasetchanged

如何在我的适配器为空时阻止应用程序崩溃。我使用notifyDataSetChanged()来更新我的列表...但是当它为空时,我的应用程序崩溃了。 我试图检查我的地图是否为空,但它不起作用。有人可以帮忙吗?

{{Dictionary.index.greeting}}

尝试计算孩子时会发生错误。

public Adapter_agenda (Context contexto, Map<String, ArrayList<Classes.Eventos>> map, ListView lista) {
    dados = map;
    this.contexto = contexto;
    this.lista = lista;
    suporte = new Suporte();
    nomes = new ArrayList<String>();
    webserver = new Webserver();
    inflater = LayoutInflater.from(contexto);
    for (Map.Entry<String, ArrayList<Classes.Eventos>> entry : dados.entrySet()) {
        nomes.add(entry.getKey());
    }
    Collections.sort(nomes);
    db = new Consultas(contexto);
    db.createDatabase();
    db.open();
    participantes = db.Consulta_Participantes(4);
    db.close();
    ids = new int[participantes.size()];
    nome_participante = new String[participantes.size()];
    for (int a = 0; a < participantes.size(); a++) {
        Classes.Participantes temp = participantes.get(a);
        nome_participante[a] = temp.nome;
        ids[a] = temp.id_participante;
    }
}

public Object getChild(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return dados.get(nomes.get(groupPosition)).get(childPosition);
}

public long getChildId(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return childPosition;
}

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.linha_listview, parent, false);
    }
    if (dados != null) {
        evento = dados.get(nomes.get(groupPosition)).get(childPosition);
        TextView Evento = (TextView)convertView.findViewById(R.id.horario_palco);
        TextView Hora = (TextView)convertView.findViewById(R.id.horario_dia);
        CheckBox add_agenda = (CheckBox) convertView.findViewById(R.id.horario_add);
        TextView eu_vou = (TextView) convertView.findViewById(R.id.horario_euvou);
        eu_vou.setText("Cancelar");
        String temp_hora;
        db.open();
        String modulo = db.Nome_modulo(evento.palco);
        db.close();
        final int id_evento = evento.id_evento;
        final int agendado = evento.agendado;
        final int hora = evento.horario;
        check = false;
        add_agenda.setChecked(agendado == 0 ? false : true);
        String temp_evento = evento.evento;
        check = false;
        Evento.setText(temp_evento);
        Hora.setText(temp_hora);
        String temp = getGroup(groupPosition).toString();
        add_agenda.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (check) {
                    db.open();              
                    if(db.Add_agenda(id_evento, isChecked)) {
                        dados.clear();
                        dados = db.Carrega_Agenda();
                        notifyDataSetChanged();
                        Toast.makeText(contexto, "Evento removido com sucesso", Toast.LENGTH_LONG).show();
                    }
                    db.close();
                }
            }
        });
        check = true;
    }
    return convertView;
}

public int getChildrenCount(int groupPosition) {
    // TODO Auto-generated method stub
    return dados.get(nomes.get(groupPosition)).size();
}

public Object getGroup(int groupPosition) {
    // TODO Auto-generated method stub
    return nomes.get(groupPosition);
}

public int getGroupCount() {
    // TODO Auto-generated method stub
    return nomes.size();
}

public long getGroupId(int groupPosition) {
    // TODO Auto-generated method stub
    return groupPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.lista_expansivel, parent, false);
    }
    TextView NomeCantor = (TextView)convertView.findViewById(R.id.lista_epx_item_label);
    NomeCantor.setText(getGroup(groupPosition).toString());
    return convertView;
}

public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return true;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {

    return true;
}

1 个答案:

答案 0 :(得分:1)

更改

public int getChildrenCount(int groupPosition) {
    // TODO Auto-generated method stub
    return dados.get(nomes.get(groupPosition)).size();
}

    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
     if(dados!=null && dados.size>0){
        return dados.get(nomes.get(groupPosition)).size();
     }else return 0;        
    }