Bart Burg
Bart Burg

Reputation: 4924

ArithmeticException when inflating TableLayout with rows and cells

Somehow I get an ArithmeticException when I use an Adapter that inflates my TableLayout. I read that you get this bug when you insert an empty TableRow into a TableLayout, while if you look at my code, it isn't empty:

public class NumberPickerAdapter {

    private final LayoutInflater inflater;
    private final LinearLayout parent;
    private final Context context;
    private final int amount;
    private View.OnClickListener onButtonClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EventBus.getDefault().postSticky(new NumberButtonClickedEvent((String) v.getTag()));
        }
    };

    public NumberPickerAdapter(Context context, LinearLayout parent, int amount) {
        inflater = LayoutInflater.from(context);
        this.parent = parent;
        this.context = context;
        this.amount = amount;
        notifyDataSetChanged();
    }


    public TableLayout getTableLayout() {
        TableLayout numberButton = (TableLayout) inflater.inflate(R.layout.table_layout_number_picker, parent, false);
        return numberButton;
    }

    public Button getButtonView(int number) {
        Button numberButton = (Button) inflater.inflate(R.layout.button_number, parent, false);
        numberButton.setText(String.valueOf(number));
        numberButton.setTag(String.valueOf(number));
        numberButton.setOnClickListener(onButtonClickListener);
        return numberButton;
    }

    public TableRow getTableRowView() {
        TableRow tableRowView = (TableRow) inflater.inflate(R.layout.table_row_number, parent, false);
        return tableRowView;
    }


    public void notifyDataSetChanged() {
        TableLayout tableLayout = getTableLayout();
        TableRow parentRow = getTableRowView();
        for(int i = 1; i <= amount; i++){
            if(i % 4 == 1 && i != 1){
                tableLayout.addView(parentRow);
                parentRow = getTableRowView();
            }
            parentRow.addView(getButtonView(i));
        }
        tableLayout.addView(parentRow);
        parent.addView(tableLayout);
    }
}

Where I call the adapter like this:

numberPickDialog = new AlertDialog.Builder(this).create();
LinearLayout customView = (LinearLayout) View.inflate(this, R.layout.dialog_pick_number, null);
numberPickDialog.setView(customView);
new NumberPickerAdapter(this, customView, 20);
numberPickDialog.show();

The error occurs after:

numberPickDialog.show();

Full log I get:

05-19 11:25:53.259  32518-32518/raakict.android.growthapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: raakict.android.growthapp, PID: 32518
    java.lang.ArithmeticException: divide by zero
            at android.widget.TableLayout.mutateColumnsWidth(TableLayout.java:587)
            at android.widget.TableLayout.shrinkAndStretchColumns(TableLayout.java:576)
            at android.widget.TableLayout.measureVertical(TableLayout.java:474)
            at android.widget.TableLayout.onMeasure(TableLayout.java:439)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1148)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            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:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

EDIT

Without being able to explain it, I replaced the NotifyDatasetChanged algorithm and it works:

public void notifyDataSetChanged() {
    TableLayout layout = getTableLayout();
    for (int row = 0; row < amount / cols; ++row) {
        TableRow rowView = getTableRowView(layout);
        for (int col = 0; col < cols; ++col) {
            rowView.addView(getButtonView(rowView, row * cols + col + 1));
        }
        layout.addView(rowView);
    }
    parent.addView(layout);
}

Upvotes: 2

Views: 341

Answers (1)

Bart Burg
Bart Burg

Reputation: 4924

With a lot of rubber duck debugging and some help from a colleague, we found what was wrong.

Apparently I changed more than I thought. There was nothing wrong with either algorithms, both work fine but the latter might be a bit better since you can set the amount of columns you want.

The problem I had was in the inflating code. I constantly used the parent LinearLayout, which also made that layout the parent instead of the RowLayout / TableLayout I eventually wanted to add it to.

So the I changed the complete adapter to this:

public class NumberPickerAdapter {

    private final LayoutInflater inflater;
    private final LinearLayout parent;
    private final Context context;
    private final int amount;
    private final int cols;
    private View.OnClickListener onButtonClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EventBus.getDefault().postSticky(new NumberButtonClickedEvent((String) v.getTag()));
        }
    };

    public NumberPickerAdapter(Context context, LinearLayout parent, int amount, int cols) {
        inflater = LayoutInflater.from(context);
        this.parent = parent;
        this.context = context;
        this.amount = amount;
        this.cols = cols;
        notifyDataSetChanged();
    }


    public TableLayout getTableLayout() {
        TableLayout numberButton = (TableLayout) inflater.inflate(R.layout.table_layout_number_picker, parent, false);
        return numberButton;
    }

    public Button getButtonView(ViewGroup parent, int number) {
        Button numberButton;
        else
            numberButton = (Button) inflater.inflate(R.layout.button_number, parent, false);
        numberButton.setText(String.valueOf(number));
        numberButton.setTag(String.valueOf(number));
        numberButton.setOnClickListener(onButtonClickListener);
        return numberButton;
    }

    public TableRow getTableRowView(ViewGroup parent) {
        TableRow tableRowView = (TableRow) inflater.inflate(R.layout.table_row_number, parent, false);
        return tableRowView;
    }


    public void notifyDataSetChanged() {
        TableLayout layout = getTableLayout();
        for (int row = 0; row < amount / cols; ++row) {
            TableRow rowView = getTableRowView(layout);
            for (int col = 0; col < cols; ++col) {
                rowView.addView(getButtonView(rowView, row * cols + col + 1));
            }
            layout.addView(rowView);
        }
        parent.addView(layout);
    }
}

Now it works :)

Upvotes: 1

Related Questions