2012-01-13 193 views
0

如何更改Windows Phone 7和C#中按钮的文本? NullPointer异常也是,如果我正在更改按钮文本的权利,那么问题是什么?更改按钮文本 - Windows Phone 7

public void CreateWords(Word[] theWords) 
{ 
    listOfWords = new Button[theWords.Length]; //Button Array 
    textBlock1.Text = theWords[0].getWord(); //Totally Works 
    for (int i = 0; i < theWords.Length; i++) 
    { 
     listOfWords[i].Content = theWords[0].getWord(); //NullPointer Exception 
    } 

    for (int i = 0; i < theWords.Length; i++) 
    { 
     stackWords.Children.Add(listOfWords[i]); 
    } 
} 

回答

4

你得到NullReferenceException因为,当你创建新的按钮配置,你还没有初始化任何数组的元素(每个元素仍然是空)的。

+0

谢谢大家,:P – 2012-01-13 03:54:11

1

正如贾斯汀在前面所说,你刚刚创建了一个按钮类型的数组,你还没有添加任何按钮到数组中。你需要明确地设置数组的每个索引到一个按钮:尝试做这样的事情。

for (int i = 0; i < theWords.Length; i++) 
{ 
    listOfWords[i] = new Button(); 
    listOfWords[i].Content = theWords[0].getWord(); //NullPointer Exception 
}