DbHelper中发生NullException

时间:2017-07-31 17:08:45

标签: android

当我点击按钮继续下一个问题时,我进行了测验活动,它显示了图像中显示的此错误。

enter image description here

这需要稍作改动,对于开发人员来说,这是一个小问题。但我对此错误感到震惊,并试图解决。请帮我解决代码中存在错误的位置。提前感谢:)

QuizActivity代码

public class QuizActivity extends Fragment {

List<Question> quesList;
int score=0;
int qid=0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc;
Button butNext;
 @Nullable
 @Override
 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
 container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_quiz, container, false);
    DbHelper db=new DbHelper(this.getActivity());
    quesList=db.getAllQuestions();
    currentQ=quesList.get(qid);
    txtQuestion=(TextView)view.findViewById(R.id.textViewQuiz);
    rda=(RadioButton)view.findViewById(R.id.radio0);
    rdb=(RadioButton)view.findViewById(R.id.radio1);
    rdc=(RadioButton)view.findViewById(R.id.radio2);
    butNext=(Button)view.findViewById(R.id.Qbutton);
    setQuestionView();

    butNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RadioGroup grp=(RadioGroup)v.findViewById(R.id.radioGroup1);
            RadioButton answer=
 (RadioButton)v.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.getActivity(), 
   ResultActivity.class);
                Bundle b = new Bundle();
                b.putInt("score", score); //Your score
                intent.putExtras(b); //Put your score to your next Intent
                startActivity(intent);
                getActivity().finish();
            }
        }
    });

    return view;
}


private void setQuestionView()
{
    txtQuestion.setText(currentQ.getQUESTION());
    rda.setText(currentQ.getOPTA());
    rdb.setText(currentQ.getOPTB());
    rdc.setText(currentQ.getOPTC());
    qid++;
}

DbHelper代码

   public class DbHelper extends SQLiteOpenHelper {
  private static final int DATABASE_VERSION = 1;
  // Database Name
  private static final String DATABASE_NAME = "Quiz";
  // 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("This patient has a viral illness caused by the 
 rubeola virus that is spread through coughs and sneezes. The Patient found 
the symptoms Spotty red/brown rash, Fever, Cough/cold-like symptoms, Greyish 
white spots in the mouth and throat. " +
            "What disease would you diagnose in patient?","Chicken Pox", 
"Measles", "Rashes", "Measles");
    this.addQuestion(q1);
    Question q2=new Question("This patient has a viral illness characterized 
by a very itchy red rash and the rash progressed from red bumps to fluid-
filled 
blisters (vesicles) that drain and scab over. " +
            "What disease would you diagnose in patient?", "Measles", 
"Rash", "Chicken Pox", "Chicken Pox");
    this.addQuestion(q2);
    Question q3=new Question("This patient has an infectious bacterial fever 
    with an eruption of red spots on the chest and abdomen and severe 
   intestinal 
     irritation which is caused by the Salmonella typhi bacteria." +
            "What disease would you diagnose in patient?","Typhoid Fever", 
    "Remittent Fever","Pel-Ebstein Fever","Typhoid Fever");
    this.addQuestion(q3);
    Question q4=new Question("This patient has a highly contagious infection 
   spread by a paramyxovirus. The virus can travel in the air through coughs 
   and sneezes, it may be on surfaces people touch, such as door handles or 
   it can be 
   picked-up from cups, cutlery, bowls or plates. The most common symptom is 
   swollen salivary glands (parotid) glands in the neck, sometimes referred 
   to  as a 
  'hamster face' appearance. The swelling can be on one or both sides of the 
    neck." +
            "What disease would you diagnose in patient?",  "Goiter", 
    "Mumps", "Thyroid","Mumps");
      this.addQuestion(q4);
      Question q5=new Question("This patient has a contagious bacterial 
      infection of the lungs and airways causing a persistent hacking cough 
      with a characteristic whooping noise.?","Wet Cough","Pertussis","Dry 
      Cough","Pertussis");
      this.addQuestion(q5);
    }
    @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;
  }
  }

1 个答案:

答案 0 :(得分:0)

从您的错误消息中,问题不在DbHelper课程中,而是在QuizActivity。似乎grp对象为空,可能您没有搜索正确的资源,或者资源丢失。

您应该检查activity_quiz.xml的内容,看看您是否真的拥有R.id.radioGroup1资源。

PS。下一次将错误堆栈作为问题中的文本包含在内,并将堆栈跟踪中显示的行号添加为代码中的注释,这有很大帮助,并避免了下注。