Java 如何解决这些logcat错误?
在我的问答游戏中,我将回合数设置为15。无论答案是对是错,游戏都不会终止。在右边回答其递增分数,否则递减。我的问答游戏运行正常,但我在其中添加了计时器,以便在计时结束时,下一个问题出现,分数降低。在定时器的完成方法中加入定时器和减量法。我的游戏只有8或9轮有效,并终止。而且分数也减少了150分。但在我的decrementScore方法中,我只将减量设置为50。如果不在计时器的完成方法中添加此方法,则其工作正常。我的代码和日志目录如下:- 提前谢谢 代码:-Java 如何解决这些logcat错误?,java,android,timer,runtime-error,Java,Android,Timer,Runtime Error,在我的问答游戏中,我将回合数设置为15。无论答案是对是错,游戏都不会终止。在右边回答其递增分数,否则递减。我的问答游戏运行正常,但我在其中添加了计时器,以便在计时结束时,下一个问题出现,分数降低。在定时器的完成方法中加入定时器和减量法。我的游戏只有8或9轮有效,并终止。而且分数也减少了150分。但在我的decrementScore方法中,我只将减量设置为50。如果不在计时器的完成方法中添加此方法,则其工作正常。我的代码和日志目录如下:- 提前谢谢 代码:- public class Questi
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());
}
}
日志类别:-
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方法停止完成循环,递减法减少每一轮的分数,而不是减少上一轮的分数。有人知道怎么解决吗。请帮助我。该视图中显示的错误是编译错误。那不是他想要的。他想要运行时错误。他做得很正确,但是他似乎没有粘贴整个logcat输出,因为他发布的所有行都不是错误行
编辑:或者更确切地说,它们是系统级错误,当系统自动处理它们时,开发人员可以忽略它们。您需要注意的是未捕获的异常,例如NullPointerException或IllegalStateException等。。。这些是应用程序级错误,需要修复。java.lang.IndexOutOfBoundsException:索引15无效,大小为15 09-06 15:30:40.191:E/AndroidRuntime3942:在com.abc.cyk.quick.GamePlay.getNextQuestionGamePlay.java:100当问题数组只有15个问题时,您尝试获取第16个问题。您向其中一个ArrayList抛出IndexOutOfBounds异常,我不能完全理解你的代码,因为我在一个电话广告上看到了它的格式,但我想这是你得到下一个问题的地方。。你加1到这一轮,如果你在开始和结束时这样做,你将超过它的指数,因为从1开始,而不是0。。。对不起,如果我的评论混淆了,对不起,是15轮。我错了,在我的解释中先写了20。它只能正常工作7或8轮。@Ryan抛出yes,这很令人困惑。@Simon这些错误有什么解决方案吗?com.abc.cyk的错误我认为是运行时错误。因为原木猫是红色的。给我一些合理的答案。