Android DbHelper中发生NullException

Android DbHelper中发生NullException,android,Android,当我点击按钮继续下一个问题时,我做了一个测验活动,它显示了这个错误,如图所示 这需要一些小改动,对于开发人员来说,这是一个小问题。但我被这个错误深深打动,并试图解决这个问题。请帮助我代码中存在的错误。提前感谢:) 活动代码 public class QuizActivity extends Fragment { List<Question> quesList; int score=0; int qid=0; Question currentQ; TextView txtQuest

当我点击按钮继续下一个问题时,我做了一个测验活动,它显示了这个错误,如图所示

这需要一些小改动,对于开发人员来说,这是一个小问题。但我被这个错误深深打动,并试图解决这个问题。请帮助我代码中存在的错误。提前感谢:)

活动代码

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++;
}
公共类quizaActivity扩展了片段{
清单问题清单;
智力得分=0;
int-qid=0;
问题Q;
文本视图TXT问题;
单选按钮rda、rdb、rdc;
按钮butNext;
@可空
@凌驾
创建视图上的公共视图(LAYOUTINGER充气机,@Nullable ViewGroup
容器,捆绑包savedInstanceState){
视图=充气机。充气(R.layout.activity\u测验,容器,错误);
DbHelper db=newdbhelper(this.getActivity());
quesList=db.getAllQuestions();
currentQ=quesList.get(qid);
txtQuestion=(TextView)view.findViewById(R.id.textViewQuike);
rda=(RadioButton)view.findViewById(R.id.radio0);
rdb=(RadioButton)view.findViewById(R.id.radio1);
rdc=(RadioButton)view.findViewById(R.id.radio2);
butNext=(Button)view.findviewbyd(R.id.Qbutton);
setQuestionView();
但是next.setOnClickListener(newview.OnClickListener(){
@凌驾
公共void onClick(视图v){
放射组grp=(放射组)v.findViewById(R.id.RadioGroup 1);
无线按钮应答=
(单选按钮)v.findViewById(grp.getCheckedRadioButtonId());
Log.d(“yourans”,currentQ.getANSWER()+“”+answer.getText());
if(currentQ.getANSWER().equals(answer.getText()))
{
分数++;
Log.d(“分数”、“你的分数”+分数);
}

如果(qid来自错误消息,则问题不在
DbHelper
类中,而是在
quizaActivity
中。似乎
grp
对象为空,可能是您没有搜索正确的资源,或者资源丢失

您应该检查
activity\u quick.xml
的内容,看看您是否真正拥有
R.id.radiogroup 1
资源


注:下次将错误堆栈作为文本包含在问题中,并将stacktrace中显示的行号作为注释添加到代码中,这非常有帮助,并避免了否决票。

我关心的是问题,而不是投票。人们应该尝试帮助,而不是反对。。是的,我是这个网站的新手,知道的不比你多。谢谢你的支持反馈,下次我也会提到线路号。@oہoوo 1588پo 1608; o 1605np,欢迎来到SO:),一开始有点难,很多人都忘记了开始时的情况,这就是我添加注释的原因。从现在开始,只要注意这些事情,你就会没事了。@ModularSynth没有回答,而是将其标记为重复。哦,如果这是重复的,请向我显示具有相同问题的相同代码。如果有人无法回答,则他/she将问题标记为重复。相同的注释适用于@0X0nosugar
   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;
  }
  }