类引用设置本身在我的代码中的某处为null

时间:2014-09-22 00:17:49

标签: java android

当我试图在newGame()中调用函数时;我得到一个Nullpointerexception,我已经理解的是我的引用_c已被设置为null,但我无法弄清楚我的代码中的任何地方它将其设置为null。除非出现问题,否则我会在这里遗漏一些东西。 我不确定是否需要展示其他课程,我不认为它们与此事有关。

这是相关的代码,我知道它很乱,可能不是很好,但这不是问题!请注意,我已经删除了代码中不相关的部分(我确定问题无法解决的部分)。

public class MainActivity extends ActionBarActivity {

private String fullWord;
Controller _c;
static String[] charArray;
MainActivity _v;

static EditText letterBox;
static EditText charBoxes;

ViewGroup layout;
ViewGroup imageLayout;
ViewGroup mainLayout;
static Button tryButton;
static  Button guessWordButton;
    Button newGameButton;
static ImageView newImg;
static EditText tempEdit;
static EditText[] etCollection;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    _v = new MainActivity();

    newImg = (ImageView) findViewById(R.id.bild1);
    newImg.setImageResource(R.drawable.bstart);
    letterBox = (EditText) findViewById(R.id.letter);

    layout = (ViewGroup) findViewById(R.id.ordet);
    imageLayout = (ViewGroup) findViewById(R.id.images);

    tryButton = (Button) findViewById(R.id.try2);
    guessWordButton = (Button) findViewById(R.id.entireword);
    newGameButton = (Button) findViewById(R.id.newgame);

    _c = new Controller(_v,letterBox);

    tryButton.setOnClickListener(_c);
    guessWordButton.setOnClickListener(_c);
    newGameButton.setOnClickListener(_c);

    buildUI();
}

public void buildUI(){
    fullWord = _c.getWord();

    charArray = fullWord.split("(?!^)");
    etCollection = new EditText[charArray.length];


    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    for (int i = 0;i<charArray.length;i++){
        charBoxes = new EditText(this); 
        charBoxes.setId(i);
        charBoxes.setLayoutParams(new LayoutParams(params)); 
        charBoxes.setFocusable(false);
        etCollection[i] = charBoxes;
        layout.addView(charBoxes);  
    }
}

public void updateWord(int placeholder, String letter){

    for (int i = 0;i<charArray.length;i++){

        if (letter.equals(charArray[i])){
            etCollection[i].setText(letter);
        }
    }   
}

public void updateImg(int imgNumber){

    newImg.setImageResource(0);


    switch (imgNumber){
    case 1:
        newImg.setImageResource(R.drawable.b1);
        break;
    case 2:
        newImg.setImageResource(R.drawable.b2);
        break;
    case 3:
        newImg.setImageResource(R.drawable.b3);
        break;
    case 4:
        newImg.setImageResource(R.drawable.b4);
        break;
    case 5:
        newImg.setImageResource(R.drawable.b5);
        break;
    case 6:
        newImg.setImageResource(R.drawable.b6);
        break;
    case 7:
        newImg.setImageResource(R.drawable.b7);
        break;
    case 8:
        newImg.setImageResource(R.drawable.bslut);
        break;
    case 9:
        tryButton.setEnabled(false);
        guessWordButton.setEnabled(false);
        newImg.setImageResource(R.drawable.bslut);
        break;
    }
}

public void newGame(){

    fullWord =  _c.getWord();
    charArray = fullWord.split("(?!^)");
}

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

1 个答案:

答案 0 :(得分:0)

我的猜测是_c.getWorld()返回一个调用方法的空对象。

因此NullPointerException