我在这个空指针异常上搜索了几个小时,但无济于事。从我所读过的内容来看,似乎还有一些尚未实例化的对象。我一遍又一遍地修改了我的代码,但我无法找到我出错的地方。如果我注释掉包含compTurn()的for循环;在onCreate();然后它进入主屏幕但显然没有执行我需要它的功能。
提前谢谢你:)
package com.hunsdale.cognitive;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class Main extends Activity {
// Initialise ArrayLists
static ArrayList<Integer> Computer;
static ArrayList<Integer> Player;
// Initialise Computer's Arraylist Iterator
static Iterator<Integer> CompIt;
// Initialise computer's pick and user's pick for arraylist
static int compPick, userPick, level;
// Initialise counter for score
static int counter;
// Initialise random number generator and boolean(for next level
// advance)
static Random rand;
// Initialise sounds for buttons
static MediaPlayer Mblue, Mgreen, Mred, Morange, Mturn;
// Initialise buttons
static ImageView blue, green, red, orange, turnSwitch;
// Initialise string to show their score to the user
TextView score;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set content = main.xml
setContentView(R.layout.main);
// Instantiate buttons
blue = (ImageView) findViewById(R.id.blue_button);
blue.setOnClickListener(onClickListener);
green = (ImageView) findViewById(R.id.green_btn);
green.setOnClickListener(onClickListener);
red = (ImageView) findViewById(R.id.red_btn);
red.setOnClickListener(onClickListener);
orange = (ImageView) findViewById(R.id.orange_btn);
orange.setOnClickListener(onClickListener);
// Instantiate button sounds
Mblue = MediaPlayer.create(Main.this, R.raw.bubble);
Mgreen = MediaPlayer.create(Main.this, R.raw.release);
Mred = MediaPlayer.create(Main.this, R.raw.texture_scratch);
Morange = MediaPlayer.create(Main.this, R.raw.whistle_scratch);
// Instantiate score display
score = (TextView) findViewById(R.id.score);
// Instantiate ArrayLists
Computer = new ArrayList<Integer>();
compPick = 0;
userPick = 0;
for (int i = 0; i <= 20; i++) {
compTurn();
}
}
public static void compTurn() {
if (Player.equals(Computer)) {
level++;
Mblue.reset();
Mgreen.reset();
Mred.reset();
Morange.reset();
Player = new ArrayList<Integer>();
counter = 0;
// the buttons cannot be clicked
blue.setEnabled(false);
green.setEnabled(false);
red.setEnabled(false);
orange.setEnabled(false);
compPick = rand.nextInt(4);
Computer.add(compPick);
for (Iterator<Integer> CompIt = Computer.iterator(); CompIt.hasNext();) {
Integer i = CompIt.next();
if (i == 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
blue.setImageResource(R.drawable.btn_blue_hghlight);
Mblue.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
blue.setImageResource(R.drawable.cst_btn_blue);
Mblue.reset();
} else if (i == 1) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
green.setImageResource(R.drawable.btn_green_highlight);
Mgreen.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
green.setImageResource(R.drawable.cst_btn_green);
Mgreen.reset();
} else if (i == 2) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
red.setImageResource(R.drawable.btn_red_highlight);
Mred.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
red.setImageResource(R.drawable.cst_btn_red);
Mred.reset();
} else if (i == 3) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
orange.setImageResource(R.drawable.btn_orange_highlight);
Morange.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
orange.setImageResource(R.drawable.big_btn_orange2);
Morange.reset();
}
}
} else if (counter < Computer.size()) {
enableUserTurn(true);
counter++;
}
}
public static void enableUserTurn(boolean buttonsEnabled) {
// enable buttons to be clicked
blue.setEnabled(buttonsEnabled);
green.setEnabled(buttonsEnabled);
red.setEnabled(buttonsEnabled);
orange.setEnabled(buttonsEnabled);
}
private OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.blue_button:
Mblue.start();
Player.add(counter, 0);
if (Player.indexOf(counter) == (Computer.indexOf(counter))) {
compTurn();
} else {
score.setText("Fail");
}
break;
case R.id.green_btn:
Mgreen.start();
Player.add(counter, 1);
if (Player.indexOf(counter) == (Computer.indexOf(counter))) {
compTurn();
} else {
score.setText("Fail");
}
break;
case R.id.red_btn:
Mred.start();
Player.add(counter, 2);
if (Player.indexOf(counter) == (Computer.indexOf(counter))) {
compTurn();
} else {
score.setText("Fail");
}
break;
case R.id.orange_btn:
Morange.start();
Player.add(counter, 3);
Morange.reset();
if (Player.indexOf(counter) == (Computer.indexOf(counter))) {
compTurn();
} else {
score.setText("Fail");
}
break;
}
}
};
}
答案 0 :(得分:1)
罪魁祸首是:
if (Player.equals(Computer))
您错过了创建对象Player
并且它为空。如果将其更改为
if (Computer.equals(Player))
它应该有用。
答案 1 :(得分:1)
您正在尝试在Player
初始化之前使用它。
这样做:
// Instantiate ArrayLists
Computer = new ArrayList<Integer>();
Player = new ArrayList<Integer>();
compPick = 0;
userPick = 0;
for (int i = 0; i <= 20; i++) {
compTurn();
}
...