方法调用get可能会产生java NullpointerException

时间:2015-09-02 18:08:59

标签: java android nullpointerexception

public class ObjectAdapter<OBJECT> extends ArrayAdapter<OBJECT> {

    public ObjectAdapterLoadMore listener;
    Context context;
    int layoutId;
    Class cellClass;

    ArrayList<Integer> apparitions = new ArrayList<Integer>();

    public ObjectAdapter(Context context, List<OBJECT> objets, int layoutId, Class<? extends ViewCell> cellClass) {
        super(context, layoutId, objets);
        this.context = context;
        this.layoutId = layoutId;
        this.cellClass = cellClass;

        //this.cells = new ViewCell[this.objects.size()];
    }

    @Override
    public int getCount() {
        int count = 0;
        try {
            count = super.getCount();
        } catch (Exception e) {
        }
        return count;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null)
            view = View.inflate(context, layoutId, null);

        ViewCell cell = null;
        if (view.getTag() != null && view.getTag() instanceof ViewCell)
            cell = (ViewCell) view.getTag();
        if (view.getTag() == null) {
            try {
                cell = (ViewCell) Class.forName(cellClass.getName()).newInstance();
                cell.construire(context, view);
            } catch (Exception e) {
                e.printStackTrace();
            }
            view.setTag(cell);
        }
        cell.construire((OBJECT) getItem(i), i);


        int apparition = apparaitre(i);
        if (apparition == 1) {
            try {
                cell.animer();
            } catch (Exception e) {
            }
        }

        if (apparition >= 1 && listener != null && getCount() > 4 && i >= (getCount() - 3)) {
            listener.loadMore();
        }

        return view;
    }

    public int apparaitre(int positon) {
        int apparition = apparitions.size() <= positon ? 0 : apparitions.get(positon);
        apparition++;
        apparitions.add(positon, apparition);
        return apparition;
    }

    public interface ObjectAdapterLoadMore {
        public void loadMore();
    }
}

以下行发生错误:

cell.construire((OBJECT) getItem(i), i);

日志中的错误文本:

  

com.sinema.fragmanlar.adapter.ObjectAdapter.getView(ObjectAdapter.java:61)   com.sothree.slidinguppanel.SlidingUpPanelLayout.onLayout(SlidingUpPanelLayout.java:708)

1 个答案:

答案 0 :(得分:0)

如果不知道您的方法construire对参数的要求,那么假设OBJECT为您抛出错误是合理的。

Object是您可能要查找的类名,除非您违反了Java约束并命名了自定义类OBJECT