将整数与构造函数一起传递

时间:2012-04-14 15:38:34

标签: java android class constructor

好的,所以

  • 创建了新的Zombie变量。
    private Zombie zombieA;
  • zombie变量初始化

    zombieA = new Zombie(1);
  • 在Zombie类中调用构造函数:

    public Zombie(int type) {
    this.type = type;
    x=200;
    y=100;
    dx=1;
    paintA.setColor(Color.RED);}
    

基本上,我希望游戏类创建一个新的Zombie,类型为1,它将通过一个开关和大小写来确定要创建哪种类型的僵尸(级别1 = 10)。问题是,当我运行它时,我的应用程序强制关闭,我得到一个错误:

zombieA = new Zombie(1);

来自最初的课程,并出现错误:

public Zombie(int type) {
来自僵尸班的

。我一次又一次地去看它,我只是看不出问题,有人注意到有什么问题吗?

2 个答案:

答案 0 :(得分:1)

没有堆栈跟踪很难说,但我怀疑问题出在这一行:

paintA.setColor(Color.RED);

您确定paintA已正确初始化吗?在我看来它是null并在你的构造函数中导致NullPointerException。如果是这种情况,请务必先将其实例化,如下所示:

paintA = new ...

答案 1 :(得分:0)

你是否在工作线程中调用Zombie类?您需要从UI线程执行此操作,此处有一些示例:Can't create handler inside thread that has not called Looper.prepare()

相关问题