2017-05-09 111 views
-1

我已经建立了一个应用程序的代码是正确的。不过我越来越Android工作室IndexOutOfBoundsException错误

java.lang.IndexOutOfBoundsException的错误:无效指数7,大小为7

每当我运行的应用程序。不知道有什么问题

我检查了很多解决方案没有帮助我。

我的代码是

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(); 
    } 

} 
} 
+1

大小开始7表示有效索引,从0-6 –

+1

哪一部分错误不清?您正尝试访问超越边界。你是否调试过并检查了你进入arrayOfListsA的内容,看看发生了什么?或者你只是从1开始而不是从0开始索引? –

回答

0

索引从0开始不在1,反正你应该得到错误的值。 它是一个很好的想法,toi为这样的事情写unitTest,以便尽早发现你的错误。

0

Java数组索引从0开始到尺寸-1(含)。

您应相应地解决您的索引:

主题= arrayOfListsA.get(1); 应该是 subject = arrayOfListsA.get(0); 等

0

替换此

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); 

随着

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); 

为索引0而不是1