单选按钮java.lang.NullPointerException

时间:2016-03-08 17:33:35

标签: java android

我正在开发一个简单的MCQ测验,下面的代码应该显示用户响应是正确还是错误,如果用户选择了错误的答案,则突出显示正确的答案。对于第一个问题它完全正常,如果我按下接下来继续下一个问题我得到这个

03-08 18:47:12.701 3230-3230 /? E / AndroidRuntime:致命异常:主要                                                  处理:com.example.useradmin.dbquiz,PID:3230                                                  显示java.lang.NullPointerException                                                      at com.example.useradmin.dbquiz.MainActivity $ 1.onCheckedChanged(MainActivity.java:109)                                                      在android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)                                                      在android.widget.RadioGroup.access $ 600(RadioGroup.java:54)                                                      在android.widget.RadioGroup $ CheckedStateTracker.onCheckedChanged(RadioGroup.java:358)                                                      在android.widget.CompoundButton.setChecked(CompoundButton.java:130)                                                      在android.widget.RadioGroup.setCheckedStateForView(RadioGroup.java:181)                                                      在android.widget.RadioGroup.check(RadioGroup.java:161)                                                      在android.widget.RadioGroup.clearCheck(RadioGroup.java:209)                                                      在com.example.useradmin.dbquiz.MainActivity $ 2.onClick(MainActivity.java:125)                                                      在android.view.View.performClick(View.java:4438)                                                      在android.view.View $ PerformClick.run(View.java:18422)                                                      在android.os.Handler.handleCallback(Handler.java:733)                                                      在android.os.Handler.dispatchMessage(Handler.java:95)                                                      在android.os.Looper.loop(Looper.java:136)                                                      在android.app.ActivityThread.main(ActivityThread.java:5001)                                                      at java.lang.reflect.Method.invokeNative(Native Method)                                                      在java.lang.reflect.Method.invoke(Method.java:515)                                                      在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)                                                      在dalvik.system.NativeStart.main(本地方法)

这是我的代码:

txtQuestion = (TextView) findViewById(R.id.txtQuestion);
    rda = (RadioButton) findViewById(R.id.rdb1);
    rdb = (RadioButton) findViewById(R.id.rdb2);
    rdc = (RadioButton) findViewById(R.id.rdb3);
    rdd = (RadioButton) findViewById(R.id.rdb4);
    butNext = (Button) findViewById(R.id.btnNext);
    rbgroup = (RadioGroup) findViewById(R.id.rbGroup);
    mquestionsList = myDbHelper.getListQuestions();
    moptionsList = myDbHelper.getListOptions();
    currentQ = mquestionsList.get(question_id);
    currentOptions = moptionsList.get(option_id);

    setQuestion();


    rbgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            if (group.getCheckedRadioButtonId() != -1)
            {

               RadioButton user_response = null;
                int i = 0;


                while (user_response == null && i < group.getChildCount()) {
                    RadioButton temp = (RadioButton) group.getChildAt(i);
                    String answer = temp.getText().toString();

                    if (currentQ.getCorrect_Ans().equalsIgnoreCase(answer)) {
                        user_response = temp;
                    }

                    i++;
                }

                int checked = group.getCheckedRadioButtonId();
                RadioButton checkedBtn = (RadioButton) findViewById(checked);

                if (user_response == checkedBtn) {
                    score++;
                } else {
                    checkedBtn.setTextColor(Color.RED);
                }

                user_response.setTextColor(Color.GREEN);
                Toast.makeText(getApplicationContext(),""+score,Toast.LENGTH_SHORT).show();


            }
        }

    });

    butNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            if (question_id < mquestionsList.size()) {
                currentQ = mquestionsList.get(question_id);
                currentOptions = moptionsList.get(option_id);
                rbgroup.clearCheck();
                //rbgroup.setEnabled(false);
                setQuestion();
            }


        }
    });

}
private void setQuestion() {

    txtQuestion.setText(currentQ.getQuestion_id() + ". " + currentQ.getQuestion());

    rda.setText(currentOptions.getOpt1());
    rdb.setText(currentOptions.getOpt2());
    rdc.setText(currentOptions.getOpt3());
    rdd.setText(currentOptions.getOpt4());

    question_id++;
    option_id++;
}

错误在这一行:     user_response.setTextColor(Color.GREEN);

0 个答案:

没有答案