我是Android编程的新手,所以我正在为练习开发一个测验应用程序..每件事情都运行正常,但有一个错误,我找不到它的解决方案。问题是当第一个问题出现时,如果我不回答它并单击下一个按钮,则会给出错误java.lang.nullpointerException。我已经使用sqlite存储问题,并且我已经使用了计时器,因为时间结束所有的单选按钮被禁用。
这是我的代码:
private TextView tv_timer;
List<Question> question_list;
int score = 0;
int qid = 0;
Question currentQ;
TextView tv_question;
RadioButton rda,rdb,rdc,rdd,rde;
Button btn_next;
MyCount counter;
RadioGroup rg;
RadioButton answer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_timer = (TextView)findViewById(R.id.tv_timer);
counter = new MyCount(50000,1000);
QuizHelper qh = new QuizHelper(this);
question_list=qh.getAllQuestions();
currentQ = question_list.get(qid);
tv_question = (TextView)findViewById(R.id.tv_question);
rda = (RadioButton)findViewById(R.id.radio1);
rdb = (RadioButton)findViewById(R.id.radio2);
rdc = (RadioButton)findViewById(R.id.radio3);
rdd = (RadioButton)findViewById(R.id.radio4);
rde = (RadioButton)findViewById(R.id.radio5);
btn_next = (Button)findViewById(R.id.button1);
setQuestionView();
btn_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
try{
rda.setChecked(false);
rdb.setChecked(false);
rdc.setChecked(false);
rdd.setChecked(false);
rde.setChecked(false);
rda.setClickable(true);
rdb.setClickable(true);
rdc.setClickable(true);
rdd.setClickable(true);
rde.setClickable(true);
rg = (RadioGroup)findViewById(R.id.RadioGroup1);
answer = (RadioButton)findViewById(rg.getCheckedRadioButtonId());
if(currentQ.getANSWER().equals(answer.getText()))
{
score++;
}
if(qid < 5){
currentQ = question_list.get(qid);
setQuestionView();
}
else{
Intent i = new Intent(MainActivity.this,ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score);
i.putExtras(b);
startActivity(i);
finish();
}
}catch(Exception e){
Log.d("Error","" + e);
}
}
});
}
public void setQuestionView(){
counter.start();
tv_question.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
rdd.setText(currentQ.getOPTD());
rde.setText(currentQ.getOPTE());
qid++;
}
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
rda.setClickable(false);
rdb.setClickable(false);
rdc.setClickable(false);
rdd.setClickable(false);
rde.setClickable(false);
}
有人请帮忙:)。
这是logcat:
09-02 01:13:58.958: D/AndroidRuntime(1348): Shutting down VM
09-02 01:13:58.958: W/dalvikvm(1348): threadid=1: thread exiting with uncaught exception (group=0xb1b0aba8)
09-02 01:13:59.018: E/AndroidRuntime(1348): FATAL EXCEPTION: main
09-02 01:13:59.018: E/AndroidRuntime(1348): Process: com.example.challengeyourself, PID: 1348
09-02 01:13:59.018: E/AndroidRuntime(1348): java.lang.NullPointerException
09-02 01:13:59.018: E/AndroidRuntime(1348): at com.example.challengeyourself.MainActivity$1.onClick(MainActivity.java:84)
09-02 01:13:59.018: E/AndroidRuntime(1348): at android.view.View.performClick(View.java:4438)
09-02 01:13:59.018: E/AndroidRuntime(1348): at android.view.View$PerformClick.run(View.java:18422)
09-02 01:13:59.018: E/AndroidRuntime(1348): at android.os.Handler.handleCallback(Handler.java:733)
09-02 01:13:59.018: E/AndroidRuntime(1348): at android.os.Handler.dispatchMessage(Handler.java:95)
09-02 01:13:59.018: E/AndroidRuntime(1348): at android.os.Looper.loop(Looper.java:136)
09-02 01:13:59.018: E/AndroidRuntime(1348): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-02 01:13:59.018: E/AndroidRuntime(1348): at java.lang.reflect.Method.invokeNative(Native Method)
09-02 01:13:59.018: E/AndroidRuntime(1348): at java.lang.reflect.Method.invoke(Method.java:515)
09-02 01:13:59.018: E/AndroidRuntime(1348): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-02 01:13:59.018: E/AndroidRuntime(1348): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-02 01:13:59.018: E/AndroidRuntime(1348): at dalvik.system.NativeStart.main(Native Method)
09-02 01:14:02.588: I/Process(1348): Sending signal. PID: 1348 SIG: 9
09-02 01:14:05.648: D/dalvikvm(1374): GC_FOR_ALLOC freed 58K, 5% free 3036K/3168K, paused 169ms, total 173ms
09-02 01:14:05.698: I/dalvikvm-heap(1374): Grow heap (frag case) to 8.390MB for 5622016-byte allocation
09-02 01:14:05.748: D/dalvikvm(1374): GC_FOR_ALLOC freed 2K, 2% free 8524K/8660K, paused 42ms, total 43ms
09-02 01:14:06.328: D/gralloc_goldfish(1374): Emulator without GPU emulation detected.
答案 0 :(得分:1)
您将获得NullPointerException
if(currentQ.getANSWER().equals(answer.getText()))
因为您没有选中任何单选按钮。 因此,当时答案将为空。
在您尝试访问答案的文本之前进行检查:
if(answer != null){
if(currentQ.getANSWER().equals(answer.getText())){
//do something
}
}
希望这有帮助。
P.S:始终在onCreate
初始化所有观看次数。这样,每次单击“下一步”时,它们都会在onClick
中初始化。这不是一个好习惯。
答案 1 :(得分:0)
您是否初始化了这些变量?
Button btn_next;
像这样
btn_next= (Button)findViewById(R.id.yourbutton);
尝试将上述代码放在活动的OnCreate方法中,或者在设置按钮的列表工具之前将其设置为
btn_next= (Button)findViewById(R.id.yourbutton);
btn_next.setOnClickListener(new View.OnClickListener()..........
我认为btn_next.setOnClickListiner
正在给出nullpointer异常,因为你没有初始化它。
答案 2 :(得分:0)
在onCreate方法中初始化rg
和answer
,即在btn_next.setOnClickListener
之前,因为这些是活动布局的一部分。
即。重写为
rg = (RadioGroup)findViewById(R.id.RadioGroup1);
answer = (RadioButton)findViewById(rg.getCheckedRadioButtonId());
btn_next.setOnClickListener(new View.OnClickListener() {
并且不要忘记删除
rg = (RadioGroup)findViewById(R.id.RadioGroup1);
answer = (RadioButton)findViewById(rg.getCheckedRadioButtonId());
来自setOnClickListener