好的,我已经进行了测验测试,这是主要的测验活动课程。
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)
我怎么能找到错误?我相信它是可变初始化的东西,但我有点盲目找到它
答案 0 :(得分:0)
试试这个:
改变这个:
String qq = ""+list.get(0);
到此:
String qq = "";
if(!list.isEmpty()){
qq += list.get(0);
}
并且,改变这个:
private void nextQ() {
list.remove(0);
if (list.isEmpty()) {
anakatema();
}
}
到此:
private void nextQ() {
if(!list.isEmpty()){
list.remove(0);
} else {
anakatema();
}
}
我想知道结果;)
答案 1 :(得分:0)
您的Button
s r1,r2和r3在onCreate()
中定义,因此其范围仅限于该方法。将它们定义为字段,代码应该有效。