Java android studio中的IndexOutOfBoundsException错误
我已经建立了一个应用程序的代码是正确的。但我还是犯了一个错误 java.lang.IndexOutOfBoundsException:索引7无效,大小为7 每当我运行应用程序时。我想不出是什么问题 我检查了许多解决方案,但没有任何帮助 我的代码是Java android studio中的IndexOutOfBoundsException错误,java,android,arrays,indexoutofboundsexception,Java,Android,Arrays,Indexoutofboundsexception,我已经建立了一个应用程序的代码是正确的。但我还是犯了一个错误 java.lang.IndexOutOfBoundsException:索引7无效,大小为7 每当我运行应用程序时。我想不出是什么问题 我检查了许多解决方案,但没有任何帮助 我的代码是 public class Lecture extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { supe
public class Lecture extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lecture);
List<List<String>> arrayOfListsA= new ArrayList<List<String>>();
SharedPreferences sharedPreferences=getSharedPreferences("MyData",MODE_PRIVATE);
String username=sharedPreferences.getString("username","NA");
String password=sharedPreferences.getString("password","NA");
TextView av=(TextView)findViewById(R.id.tvavg);
double sub=0,subt=0,prd=0,sec=0,btch=0,day=0,date=0;
try {
arrayOfListsA = new HttpGetLecture().execute(username,password).get();
List<String> subject = new ArrayList<String>();
List<String> subjecttype = new ArrayList<String>();
List<String> period = new ArrayList<String>();
List<String> section = new ArrayList<String>();
List<String> batch = new ArrayList<String>();
List<String> day1 = new ArrayList<String>();
List<String> date1 = new ArrayList<String>();
GridView gridView=(GridView) findViewById(R.id.gridLect1);
subject = arrayOfListsA.get(1);
subjecttype = arrayOfListsA.get(2);
day1= arrayOfListsA.get(3);
period= arrayOfListsA.get(4);
date1= arrayOfListsA.get(5);
section= arrayOfListsA.get(6);
batch= arrayOfListsA.get(7);
/*for(int i=0;i<subject.size();i++) {
sub = sub + parseInt(subject.get(i));
} */
/* for(int j=0;j<subjecttype.size();j++){
subt=subt+ parseInt(subjecttype.get(j));
}
for(int k=0;k<period.size();k++){
prd=prd+ parseInt(period.get(k));
}
for(int l=0;l<section.size();l++){
sec=sec+ parseInt(section.get(l));
}
for(int m=0;m<batch.size();m++){
btch=btch+ parseInt(batch.get(m));
}
*/
//avg= (out / per) * 100;
//av.setText("Average Attendance :- "+ String.valueOf(avg)+" %");
gridView.setAdapter(new CustomAdapterLecture(this,subject,subjecttype,period,section,batch,day1,date1));
} catch (InterruptedException e) {
e.printStackTrace();
e.getMessage();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
公共课堂讲座扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_讲座);
List arrayOfListsA=新建ArrayList();
SharedReferences SharedReferences=GetSharedReferences(“MyData”,模式\私有);
字符串username=SharedReferences.getString(“用户名”、“NA”);
String password=SharedReferences.getString(“密码”、“NA”);
TextView av=(TextView)findViewById(R.id.tvavg);
双倍分段=0,分段=0,prd=0,秒=0,btch=0,天=0,日期=0;
试一试{
arrayOfListsA=new-httpgettouch().execute(用户名、密码).get();
列表主题=新建ArrayList();
List subjecttype=new ArrayList();
列表周期=新建ArrayList();
列表部分=新的ArrayList();
列表批处理=新建ArrayList();
List day1=新建ArrayList();
列表日期1=新的ArrayList();
GridView GridView=(GridView)findViewById(R.id.gridLect1);
subject=arrayOfListsA.get(1);
subjecttype=arrayOfListsA.get(2);
第1天=arrayOfListsA.get(3);
period=arrayOfListsA.get(4);
date1=arrayOfListsA.get(5);
section=arrayOfListsA.get(6);
batch=arrayOfListsA.get(7);
/*对于(inti=0;iJava数组索引从0开始,直到size-1(包括)
您应该相应地修复索引:
subject=arrayOfListsA.get(1);
应该是
subject=arrayOfListsA.get(0);
等等。索引从0开始,而不是从1开始,无论如何都应该得到错误的值。
为了尽早发现错误,我最好为类似的东西编写unitTest。替换此项
GridView gridView=(GridView) findViewById(R.id.gridLect1);
subject = arrayOfListsA.get(1);
subjecttype = arrayOfListsA.get(2);
day1= arrayOfListsA.get(3);
period= arrayOfListsA.get(4);
date1= arrayOfListsA.get(5);
section= arrayOfListsA.get(6);
batch= arrayOfListsA.get(7);
与
因为索引从0开始,而不是1大小7意味着有效索引从0到6。错误的哪一部分不清楚?您正在尝试访问边界之外的内容。您是否调试并检查了arrayOfListsA中的内容以了解发生了什么?或者您只是从1开始而不是从0开始索引?
GridView gridView=(GridView) findViewById(R.id.gridLect1);
subject = arrayOfListsA.get(0);
subjecttype = arrayOfListsA.get(1);
day1= arrayOfListsA.get(2);
period= arrayOfListsA.get(3);
date1= arrayOfListsA.get(4);
section= arrayOfListsA.get(5);
batch= arrayOfListsA.get(6);