gpt4 book ai didi

android - 用行和单元格填充 TableLayout 时出现 ArithmeticException

转载 作者:行者123 更新时间:2023-11-29 01:32:45 25 4
gpt4 key购买 nike

当我使用扩展我的 TableLayout 的适配器时,不知何故我得到了一个 ArithmeticException。我读到当您将一个空的 TableRow 插入 TableLayout 时会遇到此错误,而如果您查看我的代码,它不是空的:

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

最佳答案

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

显然我的改变比我想象的要多。两种算法都没有问题,都可以正常工作,但后者可能更好一些,因为您可以设置所需的列数。

我遇到的问题是膨胀代码。我经常使用父 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);
}
}

现在可以了:)

关于android - 用行和单元格填充 TableLayout 时出现 ArithmeticException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30321460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com