我制作了 Game1.java 类,其中有一个对话框,其中有 3 个选项-植物、动物、昆虫。当我点击植物时,我们在下面这一行得到 NullPointerException
String[] plant_array = getResources().getStringArray(R.array.plant_category);
my array.xml fine is here
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="category">
<item>Plant</item>
<item>Animal</item>
<item>Insect</item>
</string-array>
<string-array name="plant_category">
<item>VIKASKUMAR</item>
<item>SOHAN</item>
<item>NARENDRA</item>
<item>VISHAL</item>
<item>PRASHANT</item>
<item>VIJAYAPANDEY</item>
<item>SATYA</item>
<item>NARAYAN</item>
<item>CHOUBEY</item>
<item>PRABHA</item>
</string-array>
<string-array name="animal_category">
<item>VIKASKUMAR1</item>
<item>SOHAN1</item>
<item>NARENDRA1</item>
<item>VISHAL1</item>
<item>PRASHANT1</item>
<item>VIJAYAPANDEY1</item>
<item>SATYA1</item>
<item>NARAYAN1</item>
<item>CHOUBEY1</item>
<item>PRABHA1</item>
</string-array>
<string-array name="insect_category">
<item>VIKASKUMAR2</item>
<item>SOHAN2</item>
<item>NARENDRA2</item>
<item>VISHAL2</item>
<item>PRASHANT2</item>
<item>VIJAYAPANDEY2</item>
<item>SATYA2</item>
<item>NARAYAN2</item>
<item>CHOUBEY2</item>
<item>PRABHA2</item>
</string-array>
my Game1.java class is here
public class Game1 extends Activity implements OnClickListener {
public static String key;
private int numwrongguesses;
private boolean flag = false;
private static String TAG = "game";
private Button abtn, bbtn, cbtn, dbtn, ebtn, fbtn, gbtn, hbtn, ibtn, jbtn,
kbtn, lbtn, mbtn, nbtn, obtn, pbtn, qbtn, rbtn, sbtn, tbtn, ubtn,
vbtn, wbtn, xbtn, ybtn, zbtn, btnHint;
private ImageView imageview;
private TextView mystword, hinttextView;
static final String KEY_CATEGORY = "com.shagunstudio.hangman.category";
private static final int CATEGORY_PLANT = 0;
private static final int CATEGORY_ANIMAL = 1;
private static final int CATEGORY_INSECT = 2;
protected static final int CONTINUE = -1;
String mysteryWord;
String hint;
private int cat;
int x;
// *************************************
//HERE NULL POINTER EXCEPTION IS THERE i.e at plant_array[]
String[] plant_array = getResources().getStringArray(R.array.plant_category);
String[] plant_array_hint = getResources().getStringArray(R.array.plant_category_hint);
// ***********************************
String[] animal_array = getResources().getStringArray(R.array.animal_category);
String[] animal_array_hint = getResources().getStringArray(
R.array.animal_category_hint);
// ***************************************************************
String[] insect_array = getResources().getStringArray(
R.array.insect_category);
String[] insect_array_hint = getResources().getStringArray(
R.array.insect_category_hint);
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
imageview = (ImageView) findViewById(R.id.hangmanimageview);
abtn = (Button) findViewById(R.id.A_BUTTON);
abtn.setOnClickListener(this);
bbtn = (Button) findViewById(R.id.B_BUTTON);
bbtn.setOnClickListener(this);
cbtn = (Button) findViewById(R.id.C_BUTTON);
cbtn.setOnClickListener(this);
dbtn = (Button) findViewById(R.id.D_BUTTON);
dbtn.setOnClickListener(this);
ebtn = (Button) findViewById(R.id.E_BUTTON);
ebtn.setOnClickListener(this);
fbtn = (Button) findViewById(R.id.F_BUTTON);
fbtn.setOnClickListener(this);
gbtn = (Button) findViewById(R.id.G_BUTTON);
gbtn.setOnClickListener(this);
hbtn = (Button) findViewById(R.id.H_BUTTON);
hbtn.setOnClickListener(this);
ibtn = (Button) findViewById(R.id.I_BUTTON);
ibtn.setOnClickListener(this);
jbtn = (Button) findViewById(R.id.J_BUTTON);
jbtn.setOnClickListener(this);
kbtn = (Button) findViewById(R.id.K_BUTTON);
kbtn.setOnClickListener(this);
lbtn = (Button) findViewById(R.id.L_BUTTON);
lbtn.setOnClickListener(this);
mbtn = (Button) findViewById(R.id.M_BUTTON);
mbtn.setOnClickListener(this);
nbtn = (Button) findViewById(R.id.N_BUTTON);
nbtn.setOnClickListener(this);
obtn = (Button) findViewById(R.id.O_BUTTON);
obtn.setOnClickListener(this);
pbtn = (Button) findViewById(R.id.P_BUTTON);
pbtn.setOnClickListener(this);
qbtn = (Button) findViewById(R.id.Q_BUTTON);
qbtn.setOnClickListener(this);
rbtn = (Button) findViewById(R.id.R_BUTTON);
rbtn.setOnClickListener(this);
sbtn = (Button) findViewById(R.id.S_BUTTON);
sbtn.setOnClickListener(this);
tbtn = (Button) findViewById(R.id.T_BUTTON);
tbtn.setOnClickListener(this);
ubtn = (Button) findViewById(R.id.U_BUTTON);
ubtn.setOnClickListener(this);
vbtn = (Button) findViewById(R.id.V_BUTTON);
vbtn.setOnClickListener(this);
wbtn = (Button) findViewById(R.id.W_BUTTON);
wbtn.setOnClickListener(this);
xbtn = (Button) findViewById(R.id.X_BUTTON);
xbtn.setOnClickListener(this);
ybtn = (Button) findViewById(R.id.Y_BUTTON);
ybtn.setOnClickListener(this);
zbtn = (Button) findViewById(R.id.Z_BUTTON);
zbtn.setOnClickListener(this);
btnHint = (Button) findViewById(R.id.HINT_BUTTON);
btnHint.setOnClickListener(this);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
int category = bundle.getInt(KEY_CATEGORY);
// bundle.putInt(key, CATEGORY_PLANT);
mysteryWord = getGameByCategory(category);
Intent i = getIntent();
Bundle b = i.getExtras();
// int c = b.getInt(key);
b.putInt(KEY_CATEGORY, CONTINUE);
mystword = (TextView) findViewById(R.id.textviewhangman);
hinttextView = (TextView) findViewById(R.id.hinttextView1);
initMystWord();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.A_BUTTON:
flag = false;
updateMystWord('A');
break;
case R.id.B_BUTTON:
flag = false;
updateMystWord('B');
break;
case R.id.C_BUTTON:
flag = false;
updateMystWord('C');
break;
case R.id.D_BUTTON:
flag = false;
updateMystWord('D');
break;
case R.id.E_BUTTON:
flag = false;
updateMystWord('E');
break;
case R.id.F_BUTTON:
flag = false;
updateMystWord('F');
break;
case R.id.G_BUTTON:
flag = false;
updateMystWord('G');
break;
case R.id.H_BUTTON:
flag = false;
updateMystWord('H');
break;
case R.id.I_BUTTON:
flag = false;
updateMystWord('I');
break;
case R.id.J_BUTTON:
flag = false;
updateMystWord('J');
break;
case R.id.K_BUTTON:
flag = false;
updateMystWord('K');
break;
case R.id.L_BUTTON:
flag = false;
updateMystWord('L');
break;
case R.id.M_BUTTON:
flag = false;
updateMystWord('M');
break;
case R.id.N_BUTTON:
flag = false;
updateMystWord('N');
break;
case R.id.O_BUTTON:
flag = false;
updateMystWord('O');
break;
case R.id.P_BUTTON:
flag = false;
updateMystWord('P');
break;
case R.id.Q_BUTTON:
flag = false;
updateMystWord('Q');
break;
case R.id.R_BUTTON:
flag = false;
updateMystWord('R');
break;
case R.id.S_BUTTON:
flag = false;
updateMystWord('S');
break;
case R.id.T_BUTTON:
flag = false;
updateMystWord('T');
break;
case R.id.U_BUTTON:
flag = false;
updateMystWord('U');
break;
case R.id.V_BUTTON:
flag = false;
updateMystWord('V');
break;
case R.id.W_BUTTON:
flag = false;
updateMystWord('W');
break;
case R.id.X_BUTTON:
flag = false;
updateMystWord('X');
break;
case R.id.Y_BUTTON:
flag = false;
updateMystWord('Y');
break;
case R.id.Z_BUTTON:
flag = false;
updateMystWord('Z');
break;
case R.id.HINT_BUTTON:
flag = false;
useHint();
break;
}
}
public String underscore() {
StringBuffer buffer = new StringBuffer();
Log.d(TAG, "nameof musteryword is =" + mysteryWord);
for (int i = 0; i < mysteryWord.length(); i++) {
Log.d(TAG, "length of musteryword is =" + mysteryWord.length());
buffer.append("_ ");
}
return buffer.toString();
}
public void initMystWord() {
mystword.setText(underscore());
}
private String getGameByCategory(int category) {
String temp = null;
switch (cat) {
case CATEGORY_PLANT:
Random random1 = new Random();
x = random1.nextInt(5);
mysteryWord = plant_array[x];
//hint = plant_array_hint[x];
temp = mysteryWord;
break;
case CATEGORY_ANIMAL:
Random random2 = new Random();
x = random2.nextInt(5);
mysteryWord = animal_array[x];
//hint = animal_array_hint[x];
temp = mysteryWord;
break;
case CATEGORY_INSECT:
Random random3 = new Random();
x = random3.nextInt(5);
mysteryWord = insect_array[x];
//hint = insect_array_hint[x];
temp = mysteryWord;
break;
}
return temp;
}
}
我们得到了空指针异常的错误,当我们调试这个程序源时找不到显示调试器也不工作..请给我答复先生..我会非常感谢你