androidnullpointerexceptionlistadaptergetview

Adapter error: NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()


I cant find whats causing this null pointer, im returning rowview for child and parent. I've readed that normally its caused by some view returning null, but i cant find the error in this case. Thanks in advance for the answers

My adapter code

public class CorrAdapter extends BaseExpandableListAdapter {

private List<Correlativa> materias;
private LayoutInflater inflater = null;

public CorrAdapter(Context context, List<Correlativa> materias) {
    this.materias = materias;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getGroupCount() {
    return materias.size();
}

@Override
public int getChildrenCount(int i) {
    return materias.get(i).getCorrelativas().size();
}

@Override
public Object getGroup(int i) {
    return null;
}

@Override
public Object getChild(int i, int i1) {
    return null;
}

@Override
public long getGroupId(int i) {
    return 0;
}

@Override
public long getChildId(int i, int i1) {
    return 0;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {


    ParentHolder parentHolder;
    View rowView = view;

    if (rowView == null) {

        rowView = inflater.inflate(R.layout.expandable_parent, viewGroup, false);
        parentHolder = new ParentHolder();

        parentHolder.tvMateria = (TextView) rowView.findViewById(R.id.tvCorrMateria);
        parentHolder.tvEstado = (TextView) rowView.findViewById(R.id.tvCorrEstado);

        rowView.setTag(parentHolder);
    } else {

        parentHolder = (ParentHolder) view.getTag();

    }



    parentHolder.tvMateria.setText(materias.get(i).getNombreMateria());

    String estadoMateria = materias.get(i).getCorrelativas().get(0);

    if(estadoMateria.equals("Puede cursar") || estadoMateria.equals("Puede inscribirse")) {
        parentHolder.tvEstado.setText(estadoMateria);
        return null;
    }

    else
        parentHolder.tvEstado.setText("No cumple correlativas");


    return rowView;
}

@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {

    View rowView = view;
    ChildHolder childHolder;

    if (rowView == null) {

        rowView = inflater.inflate(R.layout.expandable_child, viewGroup, false);
        childHolder = new ChildHolder();

        childHolder.tvCorr = (TextView) rowView.findViewById(R.id.tvCorr2);
        rowView.setTag(childHolder);

    } else {

        childHolder = (ChildHolder) rowView.getTag();

    }

    childHolder.tvCorr.setText(materias.get(i).getCorrelativas().get(i1));

    return rowView;
}

@Override
public boolean isChildSelectable(int i, int i1) {
    return false;
}

private static class ParentHolder {
    TextView tvMateria, tvEstado;
}

private static class ChildHolder {
    TextView tvCorr;
}

Logcat error

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.technocell.myapplication, PID: 27851
                                                                          java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference
                                                                              at android.widget.AbsListView.obtainView(AbsListView.java:2388)
                                                                              at android.widget.ListView.measureHeightOfChildren(ListView.java:1326)
                                                                              at android.widget.ListView.onMeasure(ListView.java:1233)
                                                                              at android.view.View.measure(View.java:19857)
                                                                              at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                                                                              at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                                              at android.view.View.measure(View.java:19857)
                                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                              at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                              at android.view.View.measure(View.java:19857)
                                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                              at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)
                                                                              at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:786)
                                                                              at android.view.View.measure(View.java:19857)
                                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                              at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                              at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                              at android.view.View.measure(View.java:19857)
                                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                              at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
                                                                              at android.view.View.measure(View.java:19857)
                                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                              at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                              at android.view.View.measure(View.java:19857)
                                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                              at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                              at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                              at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                              at android.view.View.measure(View.java:19857)
                                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                              at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                              at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
                                                                              at android.view.View.measure(View.java:19857)
                                                                              at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
                                                                              at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
                                                                              at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
                                                                              at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
                                                                              at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6338)
                                                                              at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
                                                                              at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                                                                              at android.view.Choreographer.doFrame(Choreographer.java:621)
                                                                              at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
                                                                              at android.os.Handler.handleCallback(Handler.java:751)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                              at android.os.Looper.loop(Looper.java:154)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Solution

  • In method getGroupView you have these lines:

    if(estadoMateria.equals("Puede cursar") || estadoMateria.equals("Puede inscribirse")) {
        parentHolder.tvEstado.setText(estadoMateria);
        return null;
    }
    

    I think it is the cause that sometimes null will be returned. Fix it like this:

    if(estadoMateria.equals("Puede cursar") || estadoMateria.equals("Puede inscribirse")) {
        parentHolder.tvEstado.setText(estadoMateria);
    }