应用程序在第6个条目完成时关闭

时间:2013-12-26 09:41:17

标签: android eclipse sqlite

Iam尝试使用SQLite创建一个IQTest应用程序。现在我已经添加了5个问题。但是,当我添加第6个问题并运行项目时,应用程序强制关闭。可以有人帮忙吗?

这是Question.java

package com.example.iqtest;
public class Question {
public Question()
{
}
public Question( String question, String opta, String optb, String optc,
        String answer) {
    super();
    this.question=question;
    this.opta=opta;
    this.optb=optb;
    this.optc=optc;
    this.answer=answer;
}
public int getid()
{
    return this.id;
}
public void setid(int id)
{
    this.id=id;
}
public String getquestion() {
    return this.question;
}
public void setquestion(String question) {
    this.question=question;
}
public String getopta() {
    return this.opta;
}
public void setopta(String opta) {
    this.opta=opta;
}
public String getoptb() {
    return this.optb;
}
public void setoptb(String optb) {
    this.optb=optb;
}
public String getoptc() {
    return this.optc;
}
public void setoptc(String optc) {
    this.optc=optc;
}
public String getanswer() {
    return this.answer;
}
public void setanswer(String answer) {
    this.answer=answer;
}
}

这是DbHelper.java

package com.example.iqtest;
public class DbHelper extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "triviaQuiz";
// tasks table name
private static final String TABLE_QUEST = "quest";
// tasks Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer"; //correct option
private static final String KEY_OPTA= "opta"; //option a
private static final String KEY_OPTB= "optb"; //option b
private static final String KEY_OPTC= "optc"; //option c
private SQLiteDatabase dbase;
public DbHelper(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION); 
}
@Override
public void onCreate(SQLiteDatabase db) {
    dbase=db;
    String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
            + " TEXT, " + KEY_ANSWER+ " TEXT, "+KEY_OPTA +" TEXT, "
            +KEY_OPTB +" TEXT, "+KEY_OPTC+" TEXT)";
    db.execSQL(sql);        
    addQuestions();
    //db.close();
}
private void addQuestions()
{
    Question q1=new Question("Which company is the largest manufacturer" +
            " of network equipment?","HP", "IBM", "CISCO", "CISCO");
    this.addQuestion(q1);
    Question q2=new Question("Which of the following is NOT " +
            "an operating system?", "SuSe", "BIOS", "DOS", "BIOS");
    this.addQuestion(q2);
    Question q3=new Question("Which of the following is the fastest" +
            " writable memory?","RAM", "FLASH","Register","Register");
    this.addQuestion(q3);
    Question q4=new Question("Which of the following device" +
            " regulates internet traffic?", "Router", "Bridge", "Hub","Router");
    this.addQuestion(q4);
    Question q5=new Question("Which of the following is NOT an" +
            " interpreted language?","Ruby","Python","BASIC","BASIC");
    this.addQuestion(q5);
    Question q6=new Question("Which of the following is NOT an" +
            " interpreted languaget?","Rubyt","Pythont","BASICt","BASICt");
    this.addQuestion(q6);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
    // Create tables again
    onCreate(db);
}
// Adding new question
public void addQuestion(Question quest) {
    //SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_QUES, quest.getquestion()); 
    values.put(KEY_ANSWER, quest.getanswer());
    values.put(KEY_OPTA, quest.getopta());
    values.put(KEY_OPTB, quest.getoptb());
    values.put(KEY_OPTC, quest.getoptc());
    // Inserting Row
    dbase.insert(TABLE_QUEST, null, values);        
}
public List<Question> getAllQuestions() {
    List<Question> quesList = new ArrayList<Question>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
    dbase=this.getReadableDatabase();
    Cursor cursor = dbase.rawQuery(selectQuery, null);
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Question quest = new Question();
            quest.setid(cursor.getInt(0));
            quest.setquestion(cursor.getString(1));
            quest.setanswer(cursor.getString(2));
            quest.setopta(cursor.getString(3));
            quest.setoptb(cursor.getString(4));
            quest.setoptc(cursor.getString(5));
            quesList.add(quest);
        } while (cursor.moveToNext());
    }
    // return quest list
    return quesList;
}
public int rowcount()
{
    int row=0;
    String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    row=cursor.getCount();
    return row;
}
}

这是QuizActivity.java

package com.example.iqtest;
public class QuizActivity extends Activity {
List<Question> quesList;
int score=0;
int qid=0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc;
Button butNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);
    DbHelper db=new DbHelper(this);
    quesList=db.getAllQuestions();
    currentQ=quesList.get(qid);
    txtQuestion=(TextView)findViewById(R.id.textView1);
    rda=(RadioButton)findViewById(R.id.radio0);
    rdb=(RadioButton)findViewById(R.id.radio1);
    rdc=(RadioButton)findViewById(R.id.radio2);
    butNext=(Button)findViewById(R.id.button1);
    setQuestionView();
    butNext.setOnClickListener(new View.OnClickListener() {     
        @Override
        public void onClick(View v) {
            RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
            Log.d("yourans", currentQ.getanswer()+" "+answer.getText());
            if(currentQ.getanswer().equals(answer.getText()))
            {
                score++;
                Log.d("score", "Your score"+score);
            }
            if(qid<5){                  
                currentQ=quesList.get(qid);
                setQuestionView();
            }else{
                Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
                Bundle b = new Bundle();
                b.putInt("score", score); //Your score
                intent.putExtras(b); //Put your score to your next Intent
                startActivity(intent);
                finish();
            }
        }
    });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.quiz, menu);
    return true;
}
private void setQuestionView()
{
    txtQuestion.setText(currentQ.getquestion());
    rda.setText(currentQ.getopta());
    rdb.setText(currentQ.getoptb());
    rdc.setText(currentQ.getoptc());
    qid++;
}
}

这是ResultActivity.java

package com.example.iqtest;
public class ResultActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);
    //get rating bar object
    RatingBar bar=(RatingBar)findViewById(R.id.ratingBar1); 
    bar.setNumStars(5);
    bar.setStepSize(0.5f);
    //get text view
    TextView t=(TextView)findViewById(R.id.textResult);
    //get score
    Bundle b = getIntent().getExtras();
    int score= b.getInt("score");
    //display score
    bar.setRating(score);
    switch (score)
    {
    case 1:
    case 2: t.setText("Oopsie! Better Luck Next Time!");
    break;
    case 3:
    case 4:t.setText("Hmmmm.. Someone's been reading a lot of trivia");
    break;
    case 5:t.setText("Who are you? A trivia wizard???");
    break;
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.result, menu);
    return true;
}
}

1 个答案:

答案 0 :(得分:1)

希望这个暗示可能会对你有所帮助

您的代码包含..当您在第6个问题中转移到其他部分时。

if(qid<5){                  
            currentQ=quesList.get(qid);
            setQuestionView();
        }else{
            Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
            Bundle b = new Bundle();
            b.putInt("score", score); //Your score
            intent.putExtras(b); //Put your score to your next Intent
            startActivity(intent);
            finish();
        }

您初始化QuizActivity活动的上下文并通过意图传递它。

public QuizActivity extends Activity{
  Context context;

  onCreate(){
       context = this;
       ..........
       .........

      else{
            Intent intent = new Intent(context, ResultActivity.class);
            Bundle b = new Bundle();
            b.putInt("score", score); //Your score
            intent.putExtras(b); //Put your score to your next Intent
            startActivity(intent);
            finish();
        }

    }
}

并检查 AnroidManifest.xml 文件中是否存在 ResultActivity