除以零,同时在Android中的表格布局中动态添加列

时间:2014-07-11 11:46:36

标签: java android

我想在表中动态添加15列。我得到ArithmeticException:除以零和tablelayout.mutateColumnsWidth。当我运行应用程序时它崩溃了。 请有人帮帮我。感谢您的光临。

记录错误信息

07-11 12:58:05.530: E/AndroidRuntime(1264): FATAL EXCEPTION: main
07-11 12:58:05.530: E/AndroidRuntime(1264): java.lang.ArithmeticException: divide by zero
07-11 12:58:05.530: E/AndroidRuntime(1264):     at android.widget.TableLayout.mutateColumnsWidth(TableLayout.java:579)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at android.widget.TableLayout.shrinkAndStretchColumns(TableLayout.java:568)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at android.widget.TableLayout.measureVertical(TableLayout.java:466)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at android.widget.TableLayout.onMeasure(TableLayout.java:431)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at android.view.View.measure(View.java:8313)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at android.view.View.measure(View.java:8313)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at android.view.View.measure(View.java:8313)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at android.view.View.measure(View.java:8313)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at android.os.Looper.loop(Looper.java:123)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at java.lang.reflect.Method.invokeNative(Native Method)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at java.lang.reflect.Method.invoke(Method.java:507)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-11 12:58:05.530: E/AndroidRuntime(1264):     at dalvik.system.NativeStart.main(Native Method)

在Log cat中打印我的所有val之后我得到了Exception。

07-11 17:37:36.213: E/rows(410):  = 1
07-11 17:37:36.213: E/cols(410):  = 5
07-11 17:37:36.213: E/11111(410): 11111
07-11 17:37:36.213: E/222222(410): 222222
07-11 17:37:36.223: I/System.out(410):  strText drhgft
07-11 17:37:36.223: I/System.out(410):  strText 2014
07-11 17:37:36.223: I/System.out(410):  strText 
07-11 17:37:36.223: I/System.out(410):  strText 3.8
07-11 17:37:36.234: I/System.out(410):  strText 
07-11 17:37:36.234: E/3333333(410): 3333333
07-11 17:37:36.244: E/4444444(410): 4444444
07-11 17:37:36.244: E/55555555(410): 55555555

这是我的Full类源代码

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.performance_report);
        table_layout = (TableLayout) findViewById(R.id.tableLayout_PerformanceReport);
        BuildTable();


    }

    private void BuildTable()
    {  
String performanceQuery ="select performer_name,year" 
                +","
                +"(case when second_date='June' then ((performance_rate_one+performance_rate_two+performance_rate_three+performance_rate_four+performance_rate_five)/5) end) June"
                +","
                +"(case when second_date='July' then ((performance_rate_one+performance_rate_two+performance_rate_three+performance_rate_four+performance_rate_five)/5) end) July"
                +","
                +"(case when second_date='August' then ((performance_rate_one+performance_rate_two+performance_rate_three+performance_rate_four+performance_rate_five)/5) end) August"
                +" from performance"
                +" group by empid"
                +" order by empid";

        SQLiteDatabase sqlDatabase = databaseHelper.getWritableDatabase();
        Cursor c1 =sqlDatabase.rawQuery(performanceQuery , null);

          int rows = c1.getCount();
          Log.e("rows "," = " + rows);

          int cols = c1.getColumnCount();
          Log.e("cols "," = " + cols);

        if (c1 != null && c1.getCount() != 0) 
        {  
            Log.e("11111","11111");
            if (c1.moveToFirst())
            {   
                Log.e("222222","222222");
                // outer for loop
                  for (int i = 0; i < rows; i++) 
                  {

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

                    // inner for loop
                       for (int j = 0; j < cols; j++)
                       {
                           TextView tv = new TextView(this);
                            tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                                    LayoutParams.WRAP_CONTENT));
                            tv.setBackgroundResource(R.drawable.cell_shape);
                            tv.setPadding(5, 5, 5, 5);

                            tv.setText(c1.getString(j));

                            String strText = tv.getText().toString().trim();
                            System.out.println(" strText " + strText);

                            row.addView(tv);

 }
                       Log.e("3333333","3333333");
                       c1.moveToNext();

                       Log.e("4444444","4444444");



                     table_layout.addView(row);


                  }
                  sqlDatabase.close();
                    c1.close();
                    Log.e("55555555","55555555");
            }

        }

    }



}

1 个答案:

答案 0 :(得分:5)

检查您的LayoutParams类型,如果您将观看次数置于TableRow,则应使用TableRow.LayoutParams,并在放入TableLayout.LayoutParams时使用TableLayout