RadioGroup rg;
RadioButton rb1,rb2,rb3,rb4;
Button chk,next;
TextView que;
public static int Marks,Correct,wrong;
int flag = 0;
public static String[] question={"11*11","2+5","19/1"};
public static String[] ans={"121","7","19"};
String[] opt={"22","131","121","1111","25","7","52","70","19","1","not Possible","0"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question);
que=(TextView)findViewById(R.id.textView1);
chk=(Button)findViewById(R.id.button1);
next=(Button)findViewById(R.id.button2);
rb1=(RadioButton)findViewById(R.id.rb1);
rb2=(RadioButton)findViewById(R.id.rb2);
rb3=(RadioButton)findViewById(R.id.rb3);
rb4=(RadioButton)findViewById(R.id.rb4);
rg=(RadioGroup)findViewById(R.id.radioGroup1);
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
RadioButton uans = (RadioButton)findViewById(rg.getCheckedRadioButtonId());
String uansText = uans.getText().toString();
if(uansText.equals(ans[flag]))
{
Correct++;
}
else
{
wrong++;
}
int len = que.length()-1;
flag++;
if(flag<=len)
{
que.setText(question[flag]);
rb1.setText(opt[(flag*4)]);
rb2.setText(opt[(flag*4)+1]);
rb3.setText(opt[(flag*4)+2]);
rb4.setText(opt[(flag*4)+3]);
}
else {
Intent in = new Intent(getApplicationContext(), Result.class);
startActivity(in);
}
}
});
我正在开发一个测验应用程序,当我们按下一个按钮时,它会检查正确的答案,然后显示新的问题和选项,在完成所有问题后,它将在新的结果活动中显示结果。
当我们到达最后一个问题而不是切换到结果活动时,它会抛出一个错误行数组indexoutofbound。
帮我解决这个错误 提前谢谢你
答案 0 :(得分:1)
从if条件中删除=签名或用以下代码替换您的代码...希望此帮助
if(flag<len)
{
que.setText(question[flag]);
rb1.setText(opt[(flag*4)]);
rb2.setText(opt[(flag*4)+1]);
rb3.setText(opt[(flag*4)+2]);
rb4.setText(opt[(flag*4)+3]);
}
else {
Intent in = new Intent(getApplicationContext(), Result.class);
startActivity(in);
}
答案 1 :(得分:0)
// try this way,hope this will help you...
RadioGroup rg;
RadioButton rb1, rb2, rb3, rb4;
Button chk, next;
TextView que;
public int Correct, wrong;
int flag = 0;
public static String[] question = {"11*11", "2+5", "19/1"};
public static String[] ans = {"121", "7", "19"};
String[] opt = {"22", "131", "121", "1111", "25", "7", "52", "70", "19", "1", "not Possible", "0"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
que = (TextView) findViewById(R.id.que);
chk = (Button) findViewById(R.id.chk);
next = (Button) findViewById(R.id.next);
rb1 = (RadioButton) findViewById(R.id.rb1);
rb2 = (RadioButton) findViewById(R.id.rb2);
rb3 = (RadioButton) findViewById(R.id.rb3);
rb4 = (RadioButton) findViewById(R.id.rb4);
rg = (RadioGroup) findViewById(R.id.rg);
que.setText(question[flag]);
rb1.setText(opt[(flag * 4)]);
rb2.setText(opt[(flag * 4) + 1]);
rb3.setText(opt[(flag * 4) + 2]);
rb4.setText(opt[(flag * 4) + 3]);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
RadioButton uans = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
String uansText = uans.getText().toString();
if (uansText.equals(ans[flag])) {
Correct++;
} else {
wrong++;
}
int len = question.length;
flag++;
if (flag < len) {
que.setText(question[flag]);
rb1.setText(opt[(flag * 4)]);
rb2.setText(opt[(flag * 4) + 1]);
rb3.setText(opt[(flag * 4) + 2]);
rb4.setText(opt[(flag * 4) + 3]);
} else {
Intent in = new Intent(getApplicationContext(), Result.class);
startActivity(in);
}
}
});
chk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
RadioButton uans = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
String uansText = uans.getText().toString();
if (uansText.equals(ans[flag])) {
Toast.makeText(MainActivity.this,"Your Ans Is Correct",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this,"Your Ans Is Wrong",Toast.LENGTH_SHORT).show();
}
}
});
}