因为我的日志命令踢出一个“4”我猜它是:
word.getnewword();
那是返回空指针异常,但是为什么呢?
代码的目的是让一个类在一个单词中保存不同的字母,然后分别为单词中的每个字母设置动画,然后从类中的列表中将单词更改为随机的新单词。我对每个字母使用字符而不是单词或每个字母的字符串,这也可以吗?
package com.xyz.letters;
import ......abreviated
public class letters extends Activity {
private Animals word;
boolean gameover = false;
private AdView adView;
TextView tva;
TextView tvb;
TextView tvc;
TextView tvd;
TextView tve;
TextView tvf;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.letters);
Log.e("main", "xml loaded fine");
tva = (TextView) findViewById(R.id.texta);
tvb = (TextView) findViewById(R.id.textb);
tvc = (TextView) findViewById(R.id.textc);
tvd = (TextView) findViewById(R.id.textd);
tve = (TextView) findViewById(R.id.texte);
tvf = (TextView) findViewById(R.id.textf);
Log.e("main", "textview pre-setup complete");
Log.e("main", "4");
word.getnewword();
Log.e("main", "gotword " + word.getwordchosennumber());
setletters();
Log.e("main", "set letters ok");
......etc
我的课:-
package com.xyz.letters;
import java.util.Random;
import android.util.Log;
public class Animals {
private int wordchosen;
private int length;
private char l1;
private char l2;
private char l3;
private char l4;
private char l5;
private char l6;
public Animals() {
this.wordchosen = 0;
this.length = 3;
this.l1 = 'a';
this.l2 = 'b';
this.l3 = 'c';
this.l4 = 'a';
this.l5 = 'b';
this.l6 = 'c';
}
public void getnewword() {
Log.e("animals", "0");
Random ran = new Random();
Log.e("animals", "1");
wordchosen = ran.nextInt(2);
Log.e("animals", "2");
switch (wordchosen) {
case 0:
length = 3;
l1 = 'a';
l2 = 'n';
l3 = 't';
break;
case 1:
length = 4;
l1 = 'b';
l2 = 'a';
l3 = 'l';
l4 = 'l';
break;
case 2:
length = 3;
l1 = 'b';
l2 = 'a';
l3 = 't';
break;
default:
length = 0;
break;
}
}
public int getwordchosennumber()
{
return this.wordchosen;
}
public int getlength()
{
return this.length;
}
public int getl1()
{
return this.l1;
}
public int getl2()
{
return this.l2;
}
public int getl3()
{
return this.l3;
}
public int getl4()
{
return this.l4;
}
public int getl5()
{
return this.l5;
}
public int getl6()
{
return this.l6;
}
}