使用行和单元格

时间:2015-05-19 09:18:05

标签: android exception tablelayout tablerow

当我使用扩充我的TableLayout的适配器时,我得到一个ArithmeticException。我读到你在TableLayout中插入一个空的TableRow时会遇到这个错误,而如果你查看我的代码,它就不是空的:

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);
    }
}

我在这里调用适配器:

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();

错误发生在:

之后
numberPickDialog.show();

我得到的完整日志:

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)

修改

我无法解释它,我替换了NotifyDatasetChanged算法并且它可以工作:

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);
}

1 个答案:

答案 0 :(得分:1)

经过大量的橡皮鸭调试和同事的帮助,我们发现了什么问题。

显然,我改变的次数超出了我的想象。这两种算法没有任何问题,两者都可以正常工作,但后者可能会更好,因为您可以设置所需的列数。

我遇到的问题是膨胀代码。我经常使用父LinearLayout,它也使得布局成为父级而不是RowLayout / TableLayout,我最终想将其添加到。

所以我把完整的适配器更改为:

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);
    }
}

现在有效:)