Android应用程序崩溃,没有任何解释。

时间:2013-03-07 20:28:06

标签: java android eclipse sdk

我正在编写一个tic-tac-toe android应用程序,我正在尝试使用for循环来生成数组中的所有按钮,我将它们放在相同的大小。

然而,当我把这行代码放进去时:

for (int count = 0; count < 9; count++){
    board[count].Height(ht);
}

当我尝试运行时,应用程序崩溃,没有任何解释。

发生了什么事?

以下是完整的参考代码:

package tictacstartns;

import com.google.devtools.simple.runtime.components.android.Button;
import com.google.devtools.simple.runtime.components.Component;
import com.google.devtools.simple.runtime.components.HandlesEventDispatching;
import com.google.devtools.simple.runtime.components.android.Form;
import com.google.devtools.simple.runtime.components.android.HorizontalArrangement;
import com.google.devtools.simple.runtime.components.android.Label;
import com.google.devtools.simple.runtime.events.EventDispatcher;



public class TicTacStartNS extends Form implements HandlesEventDispatching {

   private Button btnOne;
   private Button btnTwo;
   private Button btnThree;
   private Button btnFour;
   private Button btnFive;
   private Button btnSix;
   private Button btnSeven;
   private Button btnEight;
   private Button btnNine;

   private Button btnXOchanger;
   private Button btnReset;
   private Button btnClearScore;

   private Label lblTurn;
   private Label lblSpace;
   private Label lblXWins;
   private Label lblXWinsCount;
   private Label lblOWins;
   private Label lblOWinsCount;
   private Label lblCatsGame;
   private Label lblCatsGameCount;

   int ht = 75;
   int wd = 75;
   float fs = 50.0f;

   boolean xGoesNow = true;

   Button board[]=    {btnOne,btnTwo,btnThree,btnFour,btnFive,btnSix,btnSeven,btnEight,btnNine};


      void $define() {

      HorizontalArrangement hr = new HorizontalArrangement(this);
      btnOne = new Button(hr);
      btnTwo = new Button(hr);
      btnThree = new Button (hr);

      hr = new HorizontalArrangement(this);
      board[3] = new Button(hr);
      board[4] = new Button(hr);
      board[5] = new Button(hr);

      hr = new HorizontalArrangement(this);
      board[6] = new Button(hr);
      board[7] = new Button(hr);
      board[8] = new Button(hr);

      for (int count = 0; count < 9; count++){
       board[count].Height(ht);
      }

   EventDispatcher.registerEventForDelegation(this, "JavaBridge", "Click");
}


   @Override
   public boolean dispatchEvent(Component component, String id, String eventName,
                         Object[] args) {

   if (component.equals(btnOne) && eventName.equals("Click")) {
       btnOne.Text(xGoesNow?"X":"O");
       xGoesNow = (xGoesNow?false:true);
       return true;
   } else if (component.equals(btnTwo) && eventName.equals("Click")) {
       btnTwo.Text(xGoesNow?"X":"O");
       xGoesNow = (xGoesNow?false:true);
       return true;
   } else if (component.equals(btnThree) && eventName.equals("Click")) {
       btnThree.Text(xGoesNow?"X":"O");
       xGoesNow = (xGoesNow?false:true);
       return true;
   }
   return false;
   }
}

1 个答案:

答案 0 :(得分:2)

Button board[]=    {btnOne,btnTwo,btnThree,btnFour,btnFive,btnSix,btnSeven,btnEight,btnNine};

它包含所有这些引用。但它们被初始化为null

当你这样做时

btnOne = new Button(hr);

board [0] 更新,因此保持null,在访问时会导致NPE。

这是因为虽然引用指向同一个对象,但是当您重新分配一个引用的值时,它对该引用具有特定的

快速举例:

String str = null;
String [] arr= {str};
str = "hello";
System.out.println ("The value of the reference is " + arr[0]);

输出为引用的值为null

因此,在实际分配之前,请确保要分配给数组的引用指向Object,或者执行双重赋值,一个用于btnOne,一个用于board[0]。但是,由于您已经拥有该阵列,btnOne是多余的。