【问题标题】:How to create an android table dynamically by reusing one layout?如何通过重用一个布局动态创建一个android表?
【发布时间】:2015-07-03 09:15:45
【问题描述】:

我正在制作一个应用程序来将小型数据库的内容显示为动态创建的表。我使用 init() 函数创建表 onCreate 并希望它随后填充数据。由于第一行或标题行是静态的,我决定硬编码该行,然后通过数据库循环创建其余行。问题是当我尝试运行它时它会抛出一个 IndexOutOfBoundsException 。我认为这与我用来回收布局的 removeView() 调用有关,但真的不确定。谁能向我解释我做错了什么?

初始化函数:

//Initiates the table
public void init(){

    //This part defines the layout to be used for creating new rows
    TableLayout ll = (TableLayout) findViewById(R.id.tableLayout);
    TableRow row= new TableRow(this);
    TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
    row.setLayoutParams(lp);
    TextView tdate = new TextView(this);
    TextView tweight = new TextView(this);

    //This generates the caption row
    tdate.setText("Weight");
    tdate.setPadding(3, 3, 3, 3);
    tweight.setText("Date");
    tweight.setPadding(3, 3, 3, 3);
    row.addView(tdate);
    row.addView(tweight);
    ll.addView(row,0);

    //This loop adds the database content to the table (using hardcoded values here for demonstration)
    for (int i = 1; i < 3; i++) {
        ((ViewGroup)tdate.getParent()).removeView(tdate);
        ((ViewGroup)tweight.getParent()).removeView(tweight);
        ((ViewGroup)row.getParent()).removeView(row);

        tdate.setText("20.20.2020");
        tdate.setPadding(3, 3, 3, 3);
        tweight.setText("89");
        tweight.setPadding(3, 3, 3, 3);
        row.addView(tdate);
        row.addView(tweight);
        ll.addView(row,i);
    }
}

Logcat:

07-03 11:00:34.903  30276-30276/net.sanctum.adhominem.sanctum E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: net.sanctum.adhominem.sanctum, PID: 30276
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.sanctum.adhominem.sanctum/net.sanctum.adhominem.sanctum.Log}: java.lang.IndexOutOfBoundsException: index=1 count=0
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2329)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
        at android.app.ActivityThread.access$900(ActivityThread.java:147)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
 Caused by: java.lang.IndexOutOfBoundsException: index=1 count=0
        at android.view.ViewGroup.addInArray(ViewGroup.java:3971)
        at android.view.ViewGroup.addViewInner(ViewGroup.java:3902)
        at android.view.ViewGroup.addView(ViewGroup.java:3733)
        at android.widget.TableLayout.addView(TableLayout.java:429)
        at android.view.ViewGroup.addView(ViewGroup.java:3678)
        at android.widget.TableLayout.addView(TableLayout.java:411)
        at net.sanctum.adhominem.sanctum.Log.init(Log.java:77)
        at net.sanctum.adhominem.sanctum.Log.onCreate(Log.java:20)
        at android.app.Activity.performCreate(Activity.java:5933)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)

            

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
            at android.app.ActivityThread.access$900(ActivityThread.java:147)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

【问题讨论】:

  • 它说 init() 函数的第 77 行是问题所在。查看代码,您似乎只在表中创建了一行。这可以解释为什么当您尝试访问第二行(位置 1)时它说越界。如果表始终是硬编码的,您可以将所有行放在 xml 中,每个位置具有不同的 ids/vales。如果您希望数据通过 Adapter 改变 ListView 可能更合适。

标签: android android-layout android-view indexoutofboundsexception android-tablelayout


【解决方案1】:

用这个,

//Initiates the table
public void init(){

    //This part defines the layout to be used for creating new rows
    TableLayout ll = (TableLayout) findViewById(R.id.table_layout);
    TableRow row= new TableRow(this);
    TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
    row.setLayoutParams(lp);
    TextView tdate = new TextView(this);
    TextView tweight = new TextView(this);

    //This generates the caption row
    tdate.setText("Weight");
    tdate.setPadding(3, 3, 3, 3);
    tweight.setText("Date");
    tweight.setPadding(3, 3, 3, 3);
    row.addView(tdate);
    row.addView(tweight);
    ll.addView(row,0);

    //This loop adds the database content to the table (using hardcoded values here for demonstration)
    for (int i = 1; i < 3; i++) {

        row= new TableRow(this);
        lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
        row.setLayoutParams(lp);
        tdate = new TextView(this);
        tweight = new TextView(this);
        tdate.setText("20.20.2020");
        tdate.setPadding(3, 3, 3, 3);
        tweight.setText("89");
        tweight.setPadding(3, 3, 3, 3);
        row.addView(tdate);
        row.addView(tweight);
        ll.addView(row,i);
    }
}

【讨论】:

  • 谢谢,这似乎有效。我在这里犯了什么错误?真的会帮助我在未来避免这种情况。
  • 您将行添加到表格布局中,然后从布局中删除(每次它的子计数为 0 但您给出 1、2、3),这就是它显示 AIOE 的原因。
  • 如果您在循环中将同一行添加到布局中,则显示为视图已存在请删除,因此每次添加到布局时都需要创建一个新的表格行。
猜你喜欢
  • 2012-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-25
相关资源
最近更新 更多