私はアンドロイドクイズに取り組んでいます。エミュレーターで実行すると、最初の質問が表示され、それに答えます。その動作を停止しました。そして最初からやり直します。コードのどこに問題があるのか わかりません。
questionactivity.class
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
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);
/**
* 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);
/**
* 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)));
}
@Override
public void onClick(View arg0) {
//Log.d("Questions", "Moving to next question");
if(!checkAnswer()) 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);
startActivity(i);
finish();
}
}
@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() {
String answer = getSelectedAnswer();
if (currentQ.getAnswer().equalsIgnoreCase(answer))
{
//Log.d("Questions", "Correct Answer!");
currentGame.incrementScore();
}
else{
//Log.d("Questions", "Incorrect Answer!");
currentGame.decrementScore();
}
return true;
}
/**
*
*/
private String getSelectedAnswer() {
Button c1 = (Button)findViewById(R.id.answer1);
Button c2 = (Button)findViewById(R.id.answer2);
Button c3 = (Button)findViewById(R.id.answer3);
Button c4 = (Button)findViewById(R.id.answer4);
if (c1.callOnClick())
{
return c1.getText().toString();
}
if (c2.callOnClick())
{
return c2.getText().toString();
}
if (c3.callOnClick())
{
return c3.getText().toString();
}
if (c4.callOnClick())
{
return c4.getText().toString();
}
return null;
}
}
ログ出力:
09-02 08:17:30.083: E/Trace(2148): error opening trace file: No such file or directory (2)
09-02 08:17:30.193: D/dalvikvm(2148): GC_FOR_ALLOC freed 65K, 8% free 2413K/2620K, paused 26ms, total 28ms
09-02 08:17:30.203: I/dalvikvm-heap(2148): Grow heap (frag case) to 4.553MB for 2160016-byte allocation
09-02 08:17:30.313: D/dalvikvm(2148): GC_FOR_ALLOC freed 1K, 5% free 4521K/4732K, paused 104ms, total 104ms
09-02 08:17:30.363: D/dalvikvm(2148): GC_CONCURRENT freed <1K, 5% free 4521K/4732K, paused 4ms+3ms, total 50ms
09-02 08:17:30.832: D/gralloc_goldfish(2148): Emulator without GPU emulation detected.
09-02 08:17:35.432: D/dalvikvm(2148): GC_FOR_ALLOC freed 17K, 4% free 4988K/5148K, paused 42ms, total 45ms
09-02 08:17:35.482: I/dalvikvm-heap(2148): Grow heap (frag case) to 7.068MB for 2160016-byte allocation
09-02 08:17:35.562: D/dalvikvm(2148): GC_CONCURRENT freed 4K, 3% free 7092K/7260K, paused 4ms+4ms, total 77ms
09-02 08:17:36.512: I/Choreographer(2148): Skipped 764 frames! The application may be doing too much work on its main thread.
09-02 08:17:38.682: I/dalvikvm(2148): threadid=1: stack overflow on call to Landroid/view/View;.findViewById:LI
09-02 08:17:38.682: I/dalvikvm(2148): method requires 12+20+8=40 bytes, fp is 0x4205a320 (32 left)
09-02 08:17:38.682: I/dalvikvm(2148): expanding stack end (0x4205a300 to 0x4205a000)
09-02 08:17:38.682: I/dalvikvm(2148): Shrank stack (to 0x4205a300, curFrame is 0x4205fec4)
09-02 08:17:38.682: D/AndroidRuntime(2148): Shutting down VM
09-02 08:17:38.682: W/dalvikvm(2148): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-02 08:17:39.092: E/AndroidRuntime(2148): FATAL EXCEPTION: main
09-02 08:17:39.092: E/AndroidRuntime(2148): java.lang.StackOverflowError
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.ViewGroup.findViewTraversal(ViewGroup.java:3053)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.View.findViewById(View.java:15104)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.ViewGroup.findViewTraversal(ViewGroup.java:3053)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.View.findViewById(View.java:15104)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.ViewGroup.findViewTraversal(ViewGroup.java:3053)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.View.findViewById(View.java:15104)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.ViewGroup.findViewTraversal(ViewGroup.java:3053)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.View.findViewById(View.java:15104)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.Window.findViewById(Window.java:900)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.app.Activity.findViewById(Activity.java:1839)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.starchazer.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:144)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.starchazer.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.starchazer.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.View.callOnClick(View.java:4222)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.starchazer.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.starchazer.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.starchazer.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.View.callOnClick(View.java:4222)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.View.callOnClick(View.java:4222)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.View.callOnClick(View.java:4222)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.View.callOnClick(View.java:4222)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.View.callOnClick(View.java:4222)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.View.callOnClick(View.java:4222)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.View.callOnClick(View.java:4222)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.View.callOnClick(View.java:4222)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.View.callOnClick(View.java:4222)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.View.callOnClick(View.java:4222)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
09-02 08:17:39.092: E/AndroidRuntime(2148): at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
09-02 08:17:39.092: E/AndroidRuntime(2148): at android.view.V
09-02 08:17:39.122: D/dalvikvm(2148): GC_CONCURRENT freed 762K, 11% free 7907K/8812K, paused 69ms+5ms, total 117ms
09-02 08:17:39.122: D/dalvikvm(2148): WAIT_FOR_CONCURRENT_GC blocked 16ms
09-02 08:22:39.222: I/Process(2148): Sending signal. PID: 2148 SIG: 9
09-02 08:22:39.783: E/Trace(2182): error opening trace file: No such file or directory (2)
09-02 08:22:39.883: D/dalvikvm(2182): GC_FOR_ALLOC freed 68K, 9% free 2413K/2624K, paused 25ms, total 27ms
09-02 08:22:39.893: I/dalvikvm-heap(2182): Grow heap (frag case) to 4.553MB for 2160016-byte allocation
09-02 08:22:40.013: D/dalvikvm(2182): GC_FOR_ALLOC freed 1K, 5% free 4521K/4736K, paused 114ms, total 114ms
09-02 08:22:40.063: D/dalvikvm(2182): GC_CONCURRENT freed <1K, 5% free 4521K/4736K, paused 4ms+3ms, total 49ms
09-02 08:22:40.513: D/gralloc_goldfish(2182): Emulator without GPU emulation detected.
onClick() メソッドから checkAnswer() メソッドへの呼び出しを削除すると、ゲームが機能します。しかし、私のゲームは私の答えが正しいか間違っているかをチェックしません。一方、私がこのメソッドへの呼び出しを削除していない場合。私のゲームは動作し、最初の質問に答えた後、アプリの動作が停止して再起動します。誰かが私のコードのエラーを見つけるのを手伝ってくれますか?