-2

I have been working for dynamically creating table layout but I got log error in my code java.lang.ArithmeticException: divide by zero. Code.Where I got error couldn't trigger out from logcat.

  public void servershopimagesoffline() {
     String Specifications = jsonarray;
     try {
         specifi = new JSONArray(Specifications);


     } catch (JSONException e) {
         e.printStackTrace();
     }
     if (specifi.length() > 0) {
         for (int i = 0; i < specifi.length(); i++) {
             try {
                 specification = specifi.getJSONObject(i);
             } catch (JSONException e) {
                 e.printStackTrace();
             }
             TableLayout ll = (TableLayout) findViewById(R.id.maintable);

             // header[i]=new TextView(getApplicationContext());
             try {
                 String type = specification.getString("type");
                 if (type.equalsIgnoreCase("textfield")) {

                     String mainheader = specification.getString("main_header");



                     JSONArray fileopt = specification.getJSONArray("field_option");
                     header = new TextView[fileopt.length()];

                     for (int j = 0; j < fileopt.length(); j++) {

                         JSONObject field = fileopt.getJSONObject(j);
                         row = new TableRow(this);
                         row.setLayoutParams(new TableRow.LayoutParams(
                             ViewGroup.LayoutParams.WRAP_CONTENT,
                             ViewGroup.LayoutParams.WRAP_CONTENT
                         ));

                         header[i] = new TextView(getApplicationContext());
                         String fieldoption = field.getString("inside_single_title");
                         LinearLayout.LayoutParams dim = new LinearLayout.LayoutParams(
                             LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                         header[i].setLayoutParams(dim);
                         header[i].setText(fieldoption);

                         JSONArray basicopt = field.getJSONArray("basic_opt1");
                         col = new TextView[basicopt.length()];
                         col2 = new TextView[basicopt.length()];
                         row.addView(header[i]);
                         for (int k = 0; k < basicopt.length(); k++) {
                             second = new TableRow(this);
                             second.setLayoutParams(new ViewGroup.LayoutParams(
                                 ViewGroup.LayoutParams.WRAP_CONTENT,
                                 ViewGroup.LayoutParams.WRAP_CONTENT));
                             JSONObject option = basicopt.getJSONObject(k);
                             col[k] = new TextView(getApplicationContext());
                             col2[k] = new TextView(getApplicationContext());
                             col[k].setLayoutParams(dim);
                             col2[k].setLayoutParams(dim);
                             String optnameextra = option.getString("option_name_extra");
                             col[k].setText(optnameextra);
                             String option_desc_extra = option.getString("option_desc_extra");
                             col2[k].setText(option_desc_extra);
                             second.addView(col[k]);
                             second.addView(col2[k]);

                             ll.addView(second);



                         }
                         ll.addView(row);



                     }


                 }

             } catch (JSONException e) {
                 e.printStackTrace();
             }


         }
     }


 }

Logcat Error

05 - 13 12: 10: 21.308 14880 - 14880 / com.journaldev.navigationdrawer E / AndroidRuntime: FATAL EXCEPTION: main
java.lang.ArithmeticException: divide by zero
at android.widget.TableLayout.mutateColumnsWidth(TableLayout.java: 583)
at android.widget.TableLayout.shrinkAndStretchColumns(TableLayout.java: 572)
at android.widget.TableLayout.measureVertical(TableLayout.java: 470)
at android.widget.TableLayout.onMeasure(TableLayout.java: 435)
at android.view.View.measure(View.java: 15172)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java: 1196)
at android.widget.FrameLayout.onMeasure(FrameLayout.java: 310)
at android.widget.ScrollView.onMeasure(ScrollView.java: 318)
at android.view.View.measure(View.java: 15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java: 4814)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java: 1390)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java: 1038)
at android.widget.LinearLayout.onMeasure(LinearLayout.java: 576)
at android.view.View.measure(View.java: 15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java: 4814)
at android.widget.FrameLayout.onMeasure(FrameLayout.java: 310)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java: 124)
at android.view.View.measure(View.java: 15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java: 4814)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java: 1390)
at android.widget.LinearLayout.measureVertical(LinearLayout.java: 681)
at android.widget.LinearLayout.onMeasure(LinearLayout.java: 574)
at android.view.View.measure(View.java: 15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java: 4814)
at android.widget.FrameLayout.onMeasure(FrameLayout.java: 310)
at android.view.View.measure(View.java: 15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java: 4814)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java: 1390)
at android.widget.LinearLayout.measureVertical(LinearLayout.java: 681)
at android.widget.LinearLayout.onMeasure(LinearLayout.java: 574)
at android.view.View.measure(View.java: 15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java: 4814)
at android.widget.FrameLayout.onMeasure(FrameLayout.java: 310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java: 2148)
at android.view.View.measure(View.java: 15172)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java: 1848)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java: 1100)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java: 1273)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java: 998)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java: 4212)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java: 725)
at android.view.Choreographer.doCallbacks(Choreographer.java: 555)
at android.view.Choreographer.doFrame(Choreographer.java: 525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java: 711)
at android.os.Handler.handleCallback(Handler.java: 615)
at android.os.Handler.dispatchMessage(Handler.java: 92)
at android.os.Looper.loop(Looper.java: 137)
at android.app.ActivityThread.main(ActivityThread.java: 4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java: 511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 553)
at dalvik.system.NativeStart.main(Native Method)
05 - 13 12: 15: 21.336 14880 - 14880 / com.journaldev.navigationdrawer I / Process: Sending signal.PID: 14880 SIG: 9
kani mozhi
  • 23
  • 1
  • 10

3 Answers3

1

It is not immediately obvious to me what the problem is, but I have done a bit of research and noticed the following:

The developer docs for TableLayout mention "The children of a TableLayout cannot specify the layout_widthattribute. Width is always MATCH_PARENT". You could try either not setting a width at all, or defaulting to MATCH_PARENT.

Then to follow up on that, in the source code for TableLayout, around line 583 a division is performed with the count as the divisor. Probably what is happening is that the due to you setting the width of TableLayout's children to WRAP_CONTENT, they were not correctly added, hence the count is 0.

What I would suggest otherwise is to insert a breakpoint in TableLayout's onMeasure() and see if this is indeed the case and try to avoid using WRAP_CONTENT.

Good luck and as a final remark I would also suggest to try and avoid this amount of indentation in your code. It is quite straining to read!

  • where can I change now can u pls suggested that in my code.I tried what you said correction but got same error – kani mozhi May 13 '16 at 12:55
0

I think the problem is here

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

You should change ViewGroup.LayoutParams to TableLayout.LayoutParams

Aurelian Cotuna
  • 3,076
  • 3
  • 29
  • 49
0

Finally I got answer to my question, for dyanamic row have to create Layout params

 header[i].setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT))

and for Table Layout create layout params

ll.addView(row,newTableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));`

There is certain rules of Table Layout widget if you set height must be wrap_content,width must me match parent for child layout.

kani mozhi
  • 23
  • 1
  • 10