Reputation: 185
I want to display 14 columns of an SQLite database in a table layout dynamically at run time. I have tried lots of, but when I run the app it crashes and I'm getting ArithmeticException: divide by zero and error for mutateColumnsWidth.
Here is my Activity code:
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");
}
Here is .xml code:
<?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>
Log error Information:
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)
Upvotes: 2
Views: 3453
Reputation: 127
I was having a similar error and couldn't find the problem as the logcat is not very informative. In my case, it had to do with the fact that I was adding an empty TableRow.
for (int i = from; i < until; i++) {
TableRow row = new TableRow(getApplicationContext());
table.addView(row);
//ading views to the table row
....
}
But, before adding the views, another Exception was preventing the views from being added to the TableRow. I fixed that and the problem dissapeared.
Upvotes: 0
Reputation: 41
Try this
tv.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
));
Upvotes: 4