2013-03-07 68 views
-1

我正在編寫一個tic-tac-toe android應用程序,並且我正在嘗試使用for循環來將所有按鈕放在我將它們放在相同大小的數組中。Android應用崩潰沒有解釋。

然而,當我把這個行代碼中做到這一點:

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; 
    } 
} 
+7

哦,有一個解釋。看看logcat的堆棧跟蹤並將其發佈到此處。 – 2013-03-07 20:29:31

回答

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

因此,請確保在實際分配對象之前將要分配給陣列的引用指向對象,或者執行雙賦值,一個用於btnOne,另一個用於board[0]。但是,由於您已經有陣列,因此btnOne是多餘的。