好吧,我做了一個測驗測試,這裏是主要的測驗活動課。NullPointerException Android錯誤
public class startGame extends Activity {
List<Integer> list = new LinkedList<Integer>();
CountDownTimer C1,C2;
MediaPlayer mp2,mp3;
int y = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startgame);
final Button r1 = (Button)findViewById(R.id.b1);
final Button r2 = (Button)findViewById(R.id.b2);
final Button r3 = (Button)findViewById(R.id.b3);
r1.setClickable(false);
r2.setClickable(false);
r3.setClickable(false);
anakatema();
AdView ad = (AdView)findViewById(R.id.ad);
ad.loadAd(new AdRequest());
mp2 = MediaPlayer.create(this, R.raw.correct);
mp3 = MediaPlayer.create(this, R.raw.wrong);
final TextView textic = (TextView) findViewById(R.id.myIVText2);
C1 = new CountDownTimer(31000, 1000) {
public void onTick(long millisUntilFinished) {
textic.setText("" + millisUntilFinished/1000);
}
public void onFinish() {
textic.setText("0");
if ("1".equals(myMenu.k)) { r1.setBackgroundResource(R.drawable.buttonneocorrect);}
if ("2".equals(myMenu.k)) { r2.setBackgroundResource(R.drawable.buttonneocorrect);}
if ("3".equals(myMenu.k)) { r3.setBackgroundResource(R.drawable.buttonneocorrect);}
myMenu.lives = myMenu.lives - 1;
myMenu.streak = 0;
myMenu.k = checkFinish();
}
public void onCancel() {
textic.setText("Oraios!");
}
}.start();
myMenu.k = checkFinish();
r1.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
r1.setClickable(false);
r2.setClickable(false);
r3.setClickable(false);
y=0;
if ("1".equals(myMenu.k)) {
r1.setBackgroundResource(R.drawable.buttonneocorrect);
myMenu.score = myMenu.score + 10;
myMenu.streak = myMenu.streak + 1;
if ("on".equals(myMenu.sound)) {mp2.start();}
}
else {
r1.setBackgroundResource(R.drawable.buttonneowrong);
if ("2".equals(myMenu.k)) { r2.setBackgroundResource(R.drawable.buttonneocorrect);}
if ("3".equals(myMenu.k)) { r3.setBackgroundResource(R.drawable.buttonneocorrect);}
myMenu.lives = myMenu.lives - 1;
myMenu.streak = 0;
if ("on".equals(myMenu.sound)) {mp3.start();}
}
C1.cancel();
myMenu.k = checkFinish();
}
});
r2.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
r1.setClickable(false);
r2.setClickable(false);
r3.setClickable(false);
y=0;
if ("2".equals(myMenu.k)) {
r2.setBackgroundResource(R.drawable.buttonneocorrect);
myMenu.score = myMenu.score + 10;
myMenu.streak = myMenu.streak + 1;
if ("on".equals(myMenu.sound)) {mp2.start();}
}
else {
r2.setBackgroundResource(R.drawable.buttonneowrong);
if ("1".equals(myMenu.k)) { r1.setBackgroundResource(R.drawable.buttonneocorrect);}
if ("3".equals(myMenu.k)) { r3.setBackgroundResource(R.drawable.buttonneocorrect);}
myMenu.lives = myMenu.lives - 1;
myMenu.streak = 0;
if ("on".equals(myMenu.sound)) {mp3.start();}
}
C1.cancel();
myMenu.k = checkFinish();
}
});
r3.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
r1.setClickable(false);
r2.setClickable(false);
r3.setClickable(false);
y=0;
if ("3".equals(myMenu.k)) {
r3.setBackgroundResource(R.drawable.buttonneocorrect);
myMenu.score = myMenu.score + 10;
myMenu.streak = myMenu.streak + 1;
if ("on".equals(myMenu.sound)) {mp2.start();}
}
else {
r3.setBackgroundResource(R.drawable.buttonneowrong);
if ("1".equals(myMenu.k)) { r1.setBackgroundResource(R.drawable.buttonneocorrect);}
if ("2".equals(myMenu.k)) { r2.setBackgroundResource(R.drawable.buttonneocorrect);}
myMenu.lives = myMenu.lives - 1;
myMenu.streak = 0;
if ("on".equals(myMenu.sound)) {mp3.start();}
}
C1.cancel();
myMenu.k = checkFinish();
}
});
}
public String checkFinish() {
String h="";
final AnyDBAdapter eventsData = new AnyDBAdapter(getBaseContext());
myMenu.question = myMenu.question + 1;
if (myMenu.streak == 5) {
myMenu.lives = myMenu.lives + 1;
}
String qq = ""+list.get(0);
nextQ();
eventsData.open();
final Cursor c = eventsData.ExampleSelect(qq);
this.startManagingCursor(c);
eventsData.close();
h = c.getString(5);
C2 = new CountDownTimer(2000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
if (myMenu.lives != -1) {
Button r1 = (Button)findViewById(R.id.b1);
r1.setBackgroundResource(R.drawable.buttonneo);
Button r2 = (Button)findViewById(R.id.b2);
r2.setBackgroundResource(R.drawable.buttonneo);
Button r3 = (Button)findViewById(R.id.b3);
r3.setBackgroundResource(R.drawable.buttonneo);
setQuestions(c);
c.close();
C2.cancel();
r1.setClickable(true);
r2.setClickable(true);
r3.setClickable(true);
y = 1;
}
}
}.start();
C1.start();
if (myMenu.lives == -1) {
C1.cancel();
finish();
startActivity(new Intent("dv.qtestgr.dvyzual.QuizTestActivity.ENDQUIZGAME"));
}
return h;
}
private void anakatema() {
int count = getTotalQuestions();
for (int i = 1; i <= count; i++) {
list.add(i);
}
Collections.shuffle(list);
}
private int getTotalQuestions() {
final AnyDBAdapter eventsData = new AnyDBAdapter(getBaseContext());
eventsData.open();
Cursor c = eventsData.countQuestions();
this.startManagingCursor(c);
String h = c.getString(0);
c.close();
int n = Integer.parseInt(h);
eventsData.close();
return n;
}
private void nextQ() {
list.remove(0);
if (list.isEmpty()) {
anakatema();
}
}
private void setQuestions(Cursor c) {
TextView rscore = (TextView)findViewById(R.id.q1);
rscore.setText("Σκορ: " + myMenu.score + " Ερώτηση: " + myMenu.question);
TextView rlifes = (TextView)findViewById(R.id.myIVText);
rlifes.setText("" + myMenu.lives + "");
TextView r0 = (TextView)findViewById(R.id.textView1);
r0.setText(c.getString(1));
Button r1 = (Button)findViewById(R.id.b1);
r1.setText(c.getString(2));
Button r2 = (Button)findViewById(R.id.b2);
r2.setText(c.getString(3));
Button r3 = (Button)findViewById(R.id.b3);
r3.setText(c.getString(4));
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//replaces the default 'Back' button action
if (y==0){
return true;
}
if(keyCode==KeyEvent.KEYCODE_BACK)
{
C1.cancel();
C2.cancel();
finish();
}
return true;
}
}
它的工作很好,但後遊戲結束,並開始了一段多次,我有錯誤:
03-31 23:42:47.841: E/AndroidRuntime(1519): Uncaught handler: thread main exiting due to uncaught exception
03-31 23:42:47.850: E/AndroidRuntime(1519): java.lang.NullPointerException
03-31 23:42:47.850: E/AndroidRuntime(1519): at dv.qtestgr.dvyzual.startGame$4.onClick(startGame.java:139)
03-31 23:42:47.850: E/AndroidRuntime(1519): at android.view.View.performClick(View.java:2364)
03-31 23:42:47.850: E/AndroidRuntime(1519): at android.view.View.onTouchEvent(View.java:4179)
03-31 23:42:47.850: E/AndroidRuntime(1519): at android.widget.TextView.onTouchEvent(TextView.java:6541)
03-31 23:42:47.850: E/AndroidRuntime(1519): at android.view.View.dispatchTouchEvent(View.java:3709)
03-31 23:42:47.850: E/AndroidRuntime(1519): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-31 23:42:47.850: E/AndroidRuntime(1519): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-31 23:42:47.850: E/AndroidRuntime(1519): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-31 23:42:47.850: E/AndroidRuntime(1519): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-31 23:42:47.850: E/AndroidRuntime(1519): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
03-31 23:42:47.850: E/AndroidRuntime(1519): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
03-31 23:42:47.850: E/AndroidRuntime(1519): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
03-31 23:42:47.850: E/AndroidRuntime(1519): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
03-31 23:42:47.850: E/AndroidRuntime(1519): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
03-31 23:42:47.850: E/AndroidRuntime(1519): at android.os.Handler.dispatchMessage(Handler.java:99)
03-31 23:42:47.850: E/AndroidRuntime(1519): at android.os.Looper.loop(Looper.java:123)
03-31 23:42:47.850: E/AndroidRuntime(1519): at android.app.ActivityThread.main(ActivityThread.java:4363)
03-31 23:42:47.850: E/AndroidRuntime(1519): at java.lang.reflect.Method.invokeNative(Native Method)
03-31 23:42:47.850: E/AndroidRuntime(1519): at java.lang.reflect.Method.invoke(Method.java:521)
03-31 23:42:47.850: E/AndroidRuntime(1519): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-31 23:42:47.850: E/AndroidRuntime(1519): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-31 23:42:47.850: E/AndroidRuntime(1519): at dalvik.system.NativeStart.main(Native Method)
我怎樣才能發現其中的錯誤?我相信這件事情有變量初始化,但我有點盲目找到它
第一:你看到你的應用程序有多少冗餘代碼。第二:你能突出顯示或者在異常點擊的位置放一些提示。我可以看到它的139行,但我不願意數到139行。 'at dv.qtestgr.dvyzual.startGame $ 4.onClick(startGame.java:139)' – Mayank 2012-04-01 00:35:01
thx我想我剛剛解決了,它是mediaplayer,我總是忘記.release();活動結束時。是的,我的代碼有點複雜,因爲我是新的android開發^^在約139線我叫mp2.start所以你是對的 – user1234189 2012-04-01 00:54:17