Java 在真实设备上运行时的Android IndexOutOfBoundsException
我正在开发测验应用程序,用户可以在特定时间解决测验问题,但当我在emulator上运行应用程序时,它不会出错,但在实际设备上运行应用程序时,它会抛出以下错误 错误:-Java 在真实设备上运行时的Android IndexOutOfBoundsException,java,android,sqlite,Java,Android,Sqlite,我正在开发测验应用程序,用户可以在特定时间解决测验问题,但当我在emulator上运行应用程序时,它不会出错,但在实际设备上运行应用程序时,它会抛出以下错误 错误:- java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2683) at android.app.Activi
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2683)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2744)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6195)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:411)
at com.gaurav.javascripttutorial.subactivities.QuizMainActivity.onCreate(QuizMainActivity.java:52)
public class QuizMainActivity extends AppCompatActivity {
private ProgressBar countDownTimer;
private TextView timer;
public int counter;
List<Question> quesList;
int score=0;
int qid=0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc;
Button butNext;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz_main_activity);
/**
* Quiz coding
*/
DbHelper db=new DbHelper(this);
quesList=db.getAllQuestions();
currentQ=quesList.get(qid);
txtQuestion=(TextView)findViewById(R.id.textView1);
rda=(RadioButton)findViewById(R.id.radio0);
rdb=(RadioButton)findViewById(R.id.radio1);
rdc=(RadioButton)findViewById(R.id.radio2);
butNext=(Button)findViewById(R.id.nextButton);
setQuestionView();
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton answer=(RadioButton)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<22){
currentQ=quesList.get(qid);
setQuestionView();
}else{
Intent intent = new Intent(QuizMainActivity.this, QuizResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
finish();
}
}
});
/**
* Timer Coding
*/
countDownTimer = (ProgressBar) findViewById(R.id.progressBar);
timer = (TextView) findViewById(R.id.timer);
new CountDownTimer(1200000,1000){
@Override
public void onTick(long millisUntilFinished) {
int progress = (int) (millisUntilFinished/120000)+1;
countDownTimer.setProgress(countDownTimer.getMax()-progress);
if (counter == 1080){
Toast.makeText(QuizMainActivity.this, "Only 2 minutes remaining, Please review the answers.", Toast.LENGTH_SHORT).show();
}
counter++;
Log.d("Counter number", ""+counter);
}
@Override
public void onFinish() {
Toast.makeText(QuizMainActivity.this, "Sorry! Time is over, Try again", Toast.LENGTH_LONG).show();
finish();
}
}.start();
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
private void setQuestionView()
{
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
qid++;
}
}
currentQ=quesList.get(qid);
代码:-
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2683)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2744)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6195)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:411)
at com.gaurav.javascripttutorial.subactivities.QuizMainActivity.onCreate(QuizMainActivity.java:52)
public class QuizMainActivity extends AppCompatActivity {
private ProgressBar countDownTimer;
private TextView timer;
public int counter;
List<Question> quesList;
int score=0;
int qid=0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc;
Button butNext;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz_main_activity);
/**
* Quiz coding
*/
DbHelper db=new DbHelper(this);
quesList=db.getAllQuestions();
currentQ=quesList.get(qid);
txtQuestion=(TextView)findViewById(R.id.textView1);
rda=(RadioButton)findViewById(R.id.radio0);
rdb=(RadioButton)findViewById(R.id.radio1);
rdc=(RadioButton)findViewById(R.id.radio2);
butNext=(Button)findViewById(R.id.nextButton);
setQuestionView();
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton answer=(RadioButton)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<22){
currentQ=quesList.get(qid);
setQuestionView();
}else{
Intent intent = new Intent(QuizMainActivity.this, QuizResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
finish();
}
}
});
/**
* Timer Coding
*/
countDownTimer = (ProgressBar) findViewById(R.id.progressBar);
timer = (TextView) findViewById(R.id.timer);
new CountDownTimer(1200000,1000){
@Override
public void onTick(long millisUntilFinished) {
int progress = (int) (millisUntilFinished/120000)+1;
countDownTimer.setProgress(countDownTimer.getMax()-progress);
if (counter == 1080){
Toast.makeText(QuizMainActivity.this, "Only 2 minutes remaining, Please review the answers.", Toast.LENGTH_SHORT).show();
}
counter++;
Log.d("Counter number", ""+counter);
}
@Override
public void onFinish() {
Toast.makeText(QuizMainActivity.this, "Sorry! Time is over, Try again", Toast.LENGTH_LONG).show();
finish();
}
}.start();
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
private void setQuestionView()
{
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
qid++;
}
}
currentQ=quesList.get(qid);
创建列表后,必须使用其构造函数实现它。因此,请在使用列表之前添加这一行:
quesList = new ArrayList<>();
quesList=newarraylist();
请检查gelAllQuestions()函数。首先如果函数未返回空列表,则应进行空检查。之后,您可以使用isEmpty()函数检查列表是否为空
请确保数据库中有可用数据。错误表明您的任务列表为空列表,您正试图从中获取第零个索引。您的数据库为空。首先在数据库中提出一些问题。再见,您可以尝试清除emulator中的所有应用程序数据,然后重试。可能存储了以前版本的应用程序中的一些数据,这可能“解决”了问题。如果您确定数据库中存在数据,请从设备上完全卸载应用程序,然后重新安装。@pleft感谢man,它可以正常工作。。