我想在运行时动态地在表格布局中显示14列SQLite数据库。我已经尝试了很多,但是当我运行应用程序时它崩溃了,我得到了ArithmeticException:除以零和mutateColumnsWidth的错误。
这是我的活动代码:
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);
//Cursor c1 =db.rawQuery(query, null);
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.setGravity(Gravity.CENTER);
tv.setTextSize(18);
// tv.setTextColor(010101);
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");
}
这是.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:padding="5dp" >
<TableLayout
android:id="@+id/tableLayout_PerformanceReport"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textTitle"
android:background="@android:color/white"
android:padding="10dp"
android:shrinkColumns="*"
android:stretchColumns="*"
android:scrollbars="vertical|horizontal"
android:scrollbarAlwaysDrawVerticalTrack="true">
</TableLayout>
</RelativeLayout>
记录错误信息:
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)
答案 0 :(得分:3)
试试这个
tv.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
));
答案 1 :(得分:0)
我遇到了类似的错误并且找不到问题,因为 logcat 提供的信息不是很丰富。 就我而言,这与我添加了一个空 TableRow 的事实有关。
for (int i = from; i < until; i++) {
TableRow row = new TableRow(getApplicationContext());
table.addView(row);
//ading views to the table row
....
}
但是,在添加视图之前,另一个异常阻止了将视图添加到 TableRow。我解决了这个问题,问题消失了。