我有一个名为Table
的类,我声明了这个类的一个对象,在onSizeChanged()
我初始化它,但是当我尝试在onDraw()
内引用它时,应用程序崩溃并抛出NPE
代码 :
public class GameView extends View {
private Table mTable;
....
....
protected void onDraw(Canvas canvas) {
canvas.drawText("Table_Height: "+mTable.getTableHeight(), 10, 15*
(cardArraySize_Height), textPaint);
....
....
....
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
setupMainTable(mTable);
...
...
...
}
private void setupMainTable(Table table) {
table = new Table(10,
((screenHeight/2)-(2*cardHeight)),
(screenWidth-10),
((screenHeight/2)+(2*cardHeight)));
}
答案 0 :(得分:2)
Java按值传递而不是通过引用传递。所以你需要这样做: -
private void setupMainTable() {
this.mTable= new Table(10,
((screenHeight/2)-(2*cardHeight)),
(screenWidth-10),
((screenHeight/2)+(2*cardHeight)));
}