如何解决这个Logcat错误java.lang.arithmetricException:在android中被零除
我一直在努力动态创建表布局,但我的代码java.lang.arithmetricException:除以零时出现了日志错误。 代码。我得到的错误无法从logcat中触发如何解决这个Logcat错误java.lang.arithmetricException:在android中被零除,android,android-layout,android-studio,android-tablelayout,android-tablayout,Android,Android Layout,Android Studio,Android Tablelayout,Android Tablayout,我一直在努力动态创建表布局,但我的代码java.lang.arithmetricException:除以零时出现了日志错误。 代码。我得到的错误无法从logcat中触发 public void servershopimagesoffline() { String Specifications = jsonarray; try { specifi = new JSONArray(Specifications); } catch (JSONEx
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();
}
}
}
}
我认为问题就在这里
row = new TableRow(this);
row.setLayoutParams(new TableRow.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
您应该将
ViewGroup.LayoutParams
更改为TableLayout.LayoutParams
我并不清楚问题出在哪里,但我做了一些研究并注意到以下几点:
开发人员在文档中提到“TableLayout的子项不能指定layout\u width
属性。width总是MATCH\u PARENT
”。
您可以尝试根本不设置宽度,或者默认设置为MATCH\u PARENT
接着,在第583行附近执行除法,以计数作为除数。可能发生的情况是,由于您将TableLayout的子项的宽度设置为WRAP\u CONTENT
,因此未正确添加它们,因此计数为0
否则,我建议在TableLayout的onMeasure()
中插入一个断点,看看是否确实如此,并尽量避免使用WRAP\u CONTENT
祝你好运,作为最后一句话,我还建议你尽量避免代码中出现这种缩进。读起来很吃力 最后我得到了问题的答案,因为动态行必须创建布局参数
header[i].setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT))
ll.addView(row,newTableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));`
对于表布局,创建布局参数
header[i].setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT))
ll.addView(row,newTableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));`
如果设置高度必须为wrap_content,宽度必须与子布局的父级匹配,则Table Layout窗口小部件有一定的规则。Remove
View Group
尝试此setLayoutParams(new TableRow.LayoutParams(LayoutParams.wrap_content,LayoutParams.wrap_content))代码>'row.setLayoutParams(新的TableRow.LayoutParams(显示的ViewGroup.LayoutParams.MATCH_PARENT'和相同的参数'second'@JAHydroider无法解析符号LayoutParams.WRAP_CONTENT@PetrovDmitri我遇到了相同的错误我现在可以在哪里更改?请在我的代码中建议。我尝试了您所说的更正,但遇到了相同的错误尝试设置SetStretCharlColumns(true)
在表格布局上