在我的测验游戏中,我将轮数设置为15.无论答案是错还是正确的游戏都没有终止。正确回答其增量分数否则减少。我的测验游戏工作正常,但我添加了计时器,以便下次问题的时间到来,分数减少。并在定时器的finish()方法中添加定时器和减量方法。我的游戏仅适用于8轮或9轮并终止。并且得分递减150.但是在我的减量计数()方法中,我只将减量设置为50。如果不在计时器的完成方法中添加此方法,则其工作正常。我的代码和日志猫在下面: - 提前致谢!
代码: -
public class QuestionActivity extends Activity implements OnClickListener{
private Question currentQ;
private GamePlay currentGame;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question);
processScreen();
}
private void processScreen()
{
/**
* Configure current game and get question
*/
currentGame = ((CYKApplication)getApplication()).getCurrentGame();
currentQ = currentGame.getNextQuestion();
Button nextBtn1 = (Button) findViewById(R.id.answer1);
nextBtn1.setOnClickListener(this);
Button nextBtn2 = (Button) findViewById(R.id.answer2);
nextBtn2.setOnClickListener(this);
Button nextBtn3 = (Button) findViewById(R.id.answer3);
nextBtn3.setOnClickListener(this);
Button nextBtn4 = (Button) findViewById(R.id.answer4);
nextBtn4.setOnClickListener(this);
Button nextBtn5 = (Button) findViewById(R.id.answer5);
nextBtn5.setOnClickListener(this);
/**
* Update the question and answer options..
*/
setQuestions();
}
/**
* Method to set the text for the question and answers from the current games
* current question
*/
private void setQuestions() {
//set the question text from current question
String question = Utility.capitalise(currentQ.getQuestion());
TextView qText = (TextView) findViewById(R.id.question);
qText.setText(question);
//set the available options
List<String> answers = currentQ.getQuestionOptions();
TextView option1 = (TextView) findViewById(R.id.answer1);
option1.setText(Utility.capitalise(answers.get(0)));
TextView option2 = (TextView) findViewById(R.id.answer2);
option2.setText(Utility.capitalise(answers.get(1)));
TextView option3 = (TextView) findViewById(R.id.answer3);
option3.setText(Utility.capitalise(answers.get(2)));
TextView option4 = (TextView) findViewById(R.id.answer4);
option4.setText(Utility.capitalise(answers.get(3)));
int score = currentGame.getScore();
String scr = String.valueOf(score);
TextView score1 = (TextView) findViewById(R.id.score);
score1.setText(scr);
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
TextView timers = (TextView) findViewById(R.id.timers);
timers.setText("Time: " + millisUntilFinished / 1000);
}
public void onFinish() {
currentGame.decrementScore();
processScreen();
}
}.start();
}
@Override
public void onClick(View arg0) {
//Log.d("Questions", "Moving to next question");
if(arg0.getId()==R.id.answer5)
{
new AlertDialog.Builder(this)
.setMessage("Are you sure?")
.setCancelable(true)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
finish();
}
}).setNegativeButton("No", null).show();
}
else
{
if(!checkAnswer(arg0)) return;
/**
* check if end of game
*/
if (currentGame.isGameOver()){
//Log.d("Questions", "End of game! lets add up the scores..");
//Log.d("Questions", "Questions Correct: " + currentGame.getRight());
//Log.d("Questions", "Questions Wrong: " + currentGame.getWrong());
Intent i = new Intent(this, EndgameActivity.class);
startActivity(i);
finish();
}
else{
Intent i = new Intent(this, QuestionActivity.class);
finish();
startActivity(i);
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_BACK :
return true;
}
return super.onKeyDown(keyCode, event);
}
/**
* Check if a checkbox has been selected, and if it
* has then check if its correct and update gamescore
*/
private boolean checkAnswer(View v) {
Button b=(Button) v;
String answer = b.getText().toString();
//Log.d("Questions", "Valid Checkbox selection made - check if correct");
if (currentQ.getAnswer().equalsIgnoreCase(answer))
{
b.setBackgroundResource(R.drawable.answercolor);
//Log.d("Questions", "Correct Answer!");
currentGame.incrementScore();
}
else{
b.setBackgroundResource(R.drawable.answercolorr);
//Log.d("Questions", "Incorrect Answer!");
currentGame.decrementScore();
}
return true;
}
}
分数减少和圆形增量日志cat在getNextQuestion()方法中显示一些错误的类。
public class GamePlay {
private int numRounds;
private int difficulty;
private String playerName;
private int score;
private int round;
private List<Question> questions = new ArrayList<Question>();
/**
* @return the right
*/
public int getScore() {
return score;
}
/**
* @param right the right to set
*/
public void setScore(int score) {
this.score = score;
}
/**
* @return the round
*/
public int getRound() {
return round;
}
/**
* @param round the round to set
*/
public void setRound(int round) {
this.round = round;
}
/**
* @param difficulty the difficulty to set
*/
public void setDifficulty(int difficulty) {
this.difficulty = difficulty;
}
/**
* @return the difficulty
*/
public int getDifficulty() {
return difficulty;
}
/**
* @param questions the questions to set
*/
public void setQuestions(List<Question> questions) {
this.questions = questions;
}
/**
* @param q the question to add
*/
public void addQuestions(Question q) {
this.questions.add(q);
}
/**
* @return the questions
*/
public List<Question> getQuestions() {
return questions;
}
public Question getNextQuestion(){
//get the question
Question next = questions.get(this.getRound());
//update the round number to the next round
this.setRound(this.getRound()+1);
return next;
}
/**
* method to increment the number of correct answers this game
*/
/**
* method to increment the number of incorrect answers this game
*/
public void incrementScore(){
score=score+100;
}
public void decrementScore()
{
score=score-50;
}
/**
* @param numRounds the numRounds to set
*/
public void setNumRounds(int numRounds) {
this.numRounds = numRounds;
}
/**
* @return the numRounds
*/
public int getNumRounds() {
return numRounds;
}
/**
* method that checks if the game is over
* @return boolean
*/
public boolean isGameOver(){
return (getRound() >= getNumRounds());
}
}
log cat: -
09-06 15:29:23.321: D/dalvikvm(3372): Debugger has detached; object registry had 1 entries
09-06 15:30:00.081: D/gralloc_goldfish(3942): Emulator without GPU emulation detected.
09-06 15:30:05.321: D/dalvikvm(3942): GC_FOR_ALLOC freed 39K, 7% free 2661K/2844K, paused 86ms, total 97ms
09-06 15:30:05.371: I/dalvikvm-heap(3942): Grow heap (frag case) to 4.795MB for 2160016-byte allocation
09-06 15:30:05.421: D/dalvikvm(3942): GC_FOR_ALLOC freed 1K, 4% free 4768K/4956K, paused 56ms, total 56ms
09-06 15:30:05.541: D/dalvikvm(3942): GC_CONCURRENT freed 1K, 4% free 4767K/4956K, paused 8ms+35ms, total 118ms
09-06 15:30:09.050: D/dalvikvm(3942): GC_FOR_ALLOC freed 16K, 3% free 5226K/5388K, paused 60ms, total 72ms
09-06 15:30:09.080: I/dalvikvm-heap(3942): Grow heap (frag case) to 7.300MB for 2160016-byte allocation
09-06 15:30:09.190: D/dalvikvm(3942): GC_CONCURRENT freed 242K, 6% free 7093K/7500K, paused 4ms+4ms, total 105ms
09-06 15:30:10.140: I/Choreographer(3942): Skipped 768 frames! The application may be doing too much work on its main thread.
09-06 15:30:12.360: I/Choreographer(3942): Skipped 43 frames! The application may be doing too much work on its main thread.
09-06 15:30:13.364: I/Choreographer(3942): Skipped 43 frames! The application may be doing too much work on its main thread.
09-06 15:30:16.191: I/Choreographer(3942): Skipped 53 frames! The application may be doing too much work on its main thread.
09-06 15:30:17.191: I/Choreographer(3942): Skipped 46 frames! The application may be doing too much work on its main thread.
09-06 15:30:20.225: I/Choreographer(3942): Skipped 43 frames! The application may be doing too much work on its main thread.
09-06 15:30:21.251: I/Choreographer(3942): Skipped 45 frames! The application may be doing too much work on its main thread.
09-06 15:30:22.951: I/Choreographer(3942): Skipped 62 frames! The application may be doing too much work on its main thread.
09-06 15:30:23.934: I/Choreographer(3942): Skipped 43 frames! The application may be doing too much work on its main thread.
09-06 15:30:26.042: I/Choreographer(3942): Skipped 43 frames! The application may be doing too much work on its main thread.
09-06 15:30:27.050: I/Choreographer(3942): Skipped 43 frames! The application may be doing too much work on its main thread.
09-06 15:30:28.821: I/Choreographer(3942): Skipped 43 frames! The application may be doing too much work on its main thread.
09-06 15:30:31.370: I/Choreographer(3942): Skipped 30 frames! The application may be doing too much work on its main thread.
09-06 15:30:31.830: I/Choreographer(3942): Skipped 48 frames! The application may be doing too much work on its main thread.
09-06 15:30:32.447: I/Choreographer(3942): Skipped 45 frames! The application may be doing too much work on its main thread.
09-06 15:30:33.474: I/Choreographer(3942): Skipped 43 frames! The application may be doing too much work on its main thread.
09-06 15:30:34.501: I/Choreographer(3942): Skipped 51 frames! The application may be doing too much work on its main thread.
09-06 15:30:35.981: I/Choreographer(3942): Skipped 50 frames! The application may be doing too much work on its main thread.
09-06 15:30:36.991: I/Choreographer(3942): Skipped 43 frames! The application may be doing too much work on its main thread.
09-06 15:30:38.008: I/Choreographer(3942): Skipped 45 frames! The application may be doing too much work on its main thread.
09-06 15:30:40.082: I/Choreographer(3942): Skipped 43 frames! The application may be doing too much work on its main thread.
09-06 15:30:40.181: D/AndroidRuntime(3942): Shutting down VM
09-06 15:30:40.181: W/dalvikvm(3942): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-06 15:30:40.191: E/AndroidRuntime(3942): FATAL EXCEPTION: main
09-06 15:30:40.191: E/AndroidRuntime(3942): java.lang.IndexOutOfBoundsException: Invalid index 15, size is 15
09-06 15:30:40.191: E/AndroidRuntime(3942): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
09-06 15:30:40.191: E/AndroidRuntime(3942): at java.util.ArrayList.get(ArrayList.java:304)
09-06 15:30:40.191: E/AndroidRuntime(3942): at com.abc.cyk.quiz.GamePlay.getNextQuestion(GamePlay.java:100)
09-06 15:30:40.191: E/AndroidRuntime(3942): at com.abc.cyk.QuestionActivity.processScreen(QuestionActivity.java:42)
09-06 15:30:40.191: E/AndroidRuntime(3942): at com.abc.cyk.QuestionActivity.access$0(QuestionActivity.java:36)
09-06 15:30:40.191: E/AndroidRuntime(3942): at com.abc.cyk.QuestionActivity$1.onFinish(QuestionActivity.java:98)
09-06 15:30:40.191: E/AndroidRuntime(3942): at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118)
09-06 15:30:40.191: E/AndroidRuntime(3942): at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 15:30:40.191: E/AndroidRuntime(3942): at android.os.Looper.loop(Looper.java:137)
09-06 15:30:40.191: E/AndroidRuntime(3942): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-06 15:30:40.191: E/AndroidRuntime(3942): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 15:30:40.191: E/AndroidRuntime(3942): at java.lang.reflect.Method.invoke(Method.java:511)
09-06 15:30:40.191: E/AndroidRuntime(3942): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-06 15:30:40.191: E/AndroidRuntime(3942): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-06 15:30:40.191: E/AndroidRuntime(3942): at dalvik.system.NativeStart.main(Native Method)
09-06 15:30:44.592: E/Trace(3971): error opening trace file: No such file or directory (2)
09-06 15:30:44.691: D/dalvikvm(3971): GC_FOR_ALLOC freed 36K, 7% free 2413K/2592K, paused 27ms, total 29ms
09-06 15:30:44.711: I/dalvikvm-heap(3971): Grow heap (frag case) to 4.553MB for 2160016-byte allocation
09-06 15:30:44.821: D/dalvikvm(3971): GC_FOR_ALLOC freed 1K, 4% free 4521K/4704K, paused 107ms, total 107ms
09-06 15:30:44.871: D/dalvikvm(3971): GC_CONCURRENT freed <1K, 4% free 4521K/4704K, paused 4ms+3ms, total 50ms
09-06 15:30:45.341: D/gralloc_goldfish(3971): Emulator without GPU emulation detected.
09-06 15:30:47.540: I/Choreographer(3971): Skipped 64 frames! The application may be doing too much work on its main thread.
我发现我的代码出错了。倒数计时器方法中的finish()方法停止完成循环,减量方法在每一轮减少得分而不是在时间上减少得分。有人可以知道如何解决它。请帮帮我。
答案 0 :(得分:-1)
该视图中显示的错误是编译错误。这不是他想要的。他想要运行时错误。他正在正确地做,但是他似乎没有粘贴他的整个logcat输出,因为他发布的所有行都不是错误行。
编辑:或者更确切地说,它们是系统级错误,开发人员可以忽略这些错误,因为它们由系统自动处理。您需要注意的是未捕获的异常,例如NullPointerException或IllegalStateException等...这些是应用程序级错误,您需要修复它们。