java.lang.IndexOutOfBoundsException:index = 1 count = 0

时间:2014-08-06 15:40:23

标签: android android-tablelayout

我收到此代码的java.lang.IndexOutOfBoundsException: index=1 count=0错误,我不确定为什么我会收到它。我正在尝试使用从xml字符串接收的动态数据填充表格布局。

public void createTable(String xmlForTable) 
    throws XmlPullParserException, IOException {
//public void createTable(){

    Integer rowCount = 0;
    endPlacementTag = "nothing";
    //setContentView(R.layout.activity_all_tires);

     lo = (TableLayout)  findViewById(R.id.tiresTable);

     TableRow headerRow = new TableRow(this);
     TableRow.LayoutParams hp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
     headerRow.setLayoutParams(hp);

     tv = new TextView(this);
     tv.setText("Text Header");
     headerRow.addView(tv);
     //lo.addView(headerRow);

     XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
     factory.setNamespaceAware(true);
     XmlPullParser xpp = factory.newPullParser();

     xpp.setInput(new StringReader (xmlForTable));
     int eventType = xpp.getEventType();

     TableRow row= new TableRow(this);
        TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT);
        row.setLayoutParams(lp);

       ...
        materialNumber = new TextView(this);


     for(int i=0; i < xmlForTable.length(); i ++){

        if(eventType == XmlPullParser.START_DOCUMENT) {

              System.out.println("Start document");

          } else if(eventType == XmlPullParser.END_DOCUMENT) {

              System.out.println("End document");

          } else if(eventType == XmlPullParser.START_TAG) {

              System.out.println("Start tag "+xpp.getName());
              placementTag = xpp.getName();

          } else if(eventType == XmlPullParser.END_TAG) {

              System.out.println("End tag "+xpp.getName());
              endPlacementTag = "End tag "+xpp.getName();

          } else if(eventType == XmlPullParser.TEXT) {

              System.out.println("Text "+xpp.getText());
           if(placementTag.equals("MaterialNumber"))
              {
                  materialNumber.setText(xpp.getText());
                  materialNumber.setTextSize(10);                   
                  materialNumber.setLayoutParams(new LayoutParams (LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                  /*row.addView(materialNumber);
                  row.setGravity(Gravity.CENTER);
                  row.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));*/
                  placementTag = "";

              }
              else if(endPlacementTag.equals("End tag Table")) 
              {

                  ...//other row.adds
                  row.addView(materialNumber);

                  row.setGravity(Gravity.CENTER);
                  row.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

                  //lo.addView(row, i, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                  lo.addView(row, rowCount);

                  rowCount++;
                  //i++;
                  row.removeAllViews();
                  lo.removeAllViews();

              }

              }
          eventType = xpp.next();
          }

logcat的:

08-06 11:24:00.210: E/AndroidRuntime(2979): java.lang.IndexOutOfBoundsException: index=1 count=0
08-06 11:24:00.210: E/AndroidRuntime(2979):     at android.view.ViewGroup.addInArray(ViewGroup.java:3653)
08-06 11:24:00.210: E/AndroidRuntime(2979):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3584)
08-06 11:24:00.210: E/AndroidRuntime(2979):     at android.view.ViewGroup.addView(ViewGroup.java:3415)
08-06 11:24:00.210: E/AndroidRuntime(2979):     at android.widget.TableLayout.addView(TableLayout.java:429)
08-06 11:24:00.210: E/AndroidRuntime(2979):     at android.view.ViewGroup.addView(ViewGroup.java:3360)
08-06 11:24:00.210: E/AndroidRuntime(2979):     at android.widget.TableLayout.addView(TableLayout.java:411)
08-06 11:24:00.210: E/AndroidRuntime(2979):     at cooper.tirerfid.AllTiresActivity.createTable(AllTiresActivity.java:384)
08-06 11:24:00.210: E/AndroidRuntime(2979):     at cooper.tirerfid.AllTiresActivity$1.run(AllTiresActivity.java:483)
08-06 11:24:00.210: E/AndroidRuntime(2979):     at android.os.Handler.handleCallback(Handler.java:733)
08-06 11:24:00.210: E/AndroidRuntime(2979):     at android.os.Handler.dispatchMessage(Handler.java:95)
08-06 11:24:00.210: E/AndroidRuntime(2979):     at android.os.Looper.loop(Looper.java:136)
08-06 11:24:00.210: E/AndroidRuntime(2979):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-06 11:24:00.210: E/AndroidRuntime(2979):     at java.lang.reflect.Method.invokeNative(Native Method)
08-06 11:24:00.210: E/AndroidRuntime(2979):     at java.lang.reflect.Method.invoke(Method.java:515)
08-06 11:24:00.210: E/AndroidRuntime(2979):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-06 11:24:00.210: E/AndroidRuntime(2979):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-06 11:24:00.210: E/AndroidRuntime(2979):     at dalvik.system.NativeStart.main(Native Method)

我让程序正常运行,因此它没有错误输出,但是数据表仍然不会填满数据它只是空白所以我切换它现在它只是在我运行它时崩溃了我再次陷入困境......

1 个答案:

答案 0 :(得分:0)

您正在if循环中添加子视图,因此您可以避免将位置作为第二个参数,它是可选的。      lo.addView(row,rowCount);

替换为:      lo.addView(行);

Android会处理行数。