这是我用于Android app的代码。这是数组索引出界问题即将到来。之后该应用程序崩溃。任何人都可以帮助我。
private void displayAvailableProjects() {
projectList = databaseHelper.getAllProjects();
if (projectList.size() >= 0) { //here chnages =
projectViewContainer.setVisibility(View.VISIBLE);
ArrayList<String> projects = new ArrayList<>();
projects.add("Previous Report");
for (ProjectEntity project : projectList) {
projects.add(project.getProjectName());
}
ArrayAdapter<String> projectListAdapter =
new ArrayAdapter<String>(Projectinfo.this, android.R.layout.simple_list_item_1, projects);
spinnerProject.setAdapter(projectListAdapter);
spinnerProject.setSelected(false);
} else {
projectViewContainer.setVisibility(View.GONE);
}
}
if (spinnerProject.getSelectedItemPosition() !=0)
openProjectFeedbackActivity(projectList.get(spinnerProject.getSelectedItemPosition()).getProjectId());
答案 0 :(得分:0)
if (projectList.size() >= 0)
这一行是问题所在。 您的projectList大小为0,但您正在显示列表
将其更改为
if (projectList.size() > 0)
答案 1 :(得分:0)
试试这个,
private void displayAvailableProjects() {
try{
projectList = databaseHelper.getAllProjects();
if (projectList.size() > 0) { //here chnages =
projectViewContainer.setVisibility(View.VISIBLE);
ArrayList<String> projects = new ArrayList<>();
projects.add("Previous Report");
for (ProjectEntity project : projectList) {
projects.add(project.getProjectName());
}
ArrayAdapter<String> projectListAdapter =
new ArrayAdapter<String>(Projectinfo.this,android.R.layout.simple_list_item_1, projects);
spinnerProject.setAdapter(projectListAdapter);
spinnerProject.setSelected(false);
} else {
projectViewContainer.setVisibility(View.GONE);
}
}catch(Exception e){
e.printstack();
}
}
if (spinnerProject.getSelectedItemPosition() !=0)openProjectFeedbackActivity(projectList.get(spinnerProject.getSelectedItemPosition()).getProjectId());
答案 2 :(得分:0)
projectList = databaseHelper.getAllProjects();
此行可能是您的问题。请检查您是否从数据库中获取了正确的数据。