android,为什么说var是null

时间:2017-09-02 13:02:37

标签: java android nullpointerexception

我有一个扩展类,在类中有一个var,我按值定义它,但是说这是null !!!!

header("Content-Transfer-Encoding: binary");

我按值定义了'cellStates'。但是这是null。为什么???

public class CalendarCell extends TextView
{

    public static final String TAG = "CalendarCell";

    protected ArrayList<Integer> cellStates = new ArrayList<>();

public CalendarCell(Context context)
{
    super(context);
}

public CalendarCell(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

public CalendarCell(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
}

@Override
protected int[] onCreateDrawableState(int states)
{
    int newStatesSize = cellStates.size();  // Error here (null)
    if (newStatesSize > 0)
    {

        ...
    }
    else
    {
        return super.onCreateDrawableState(states);
    }
}

public void resetStates()
{
    cellStates.clear();
}

public void addState(int state)
{
    if(!cellStates.contains(state))
        cellStates.add(state);
    }
}

1 个答案:

答案 0 :(得分:0)

cellStates最终如此:

protected final ArrayList<Integer> cellStates = new ArrayList<>();