我正在尝试开发简单的矩阵计算器。
TableLayout table = new TableLayout(this);
for (int i = 0; i < size; i++) {
TableRow row = new TableRow(this);
for (int j = 0; j < size; j++) {
EditText cell = new EditText(this);
cell.setInputType(InputType.TYPE_CLASS_NUMBER);
row.addView(cell);
}
table.addView(row);
}
这是我为输入矩阵元素创建TableLayout
的方法,但我不知道如何获取其值并将其解析为int[][]
数组以进行进一步计算。
我试着这样做
TableLayout table = new TableLayout(this);
EditText[][] cells = new EditText[size][size];
TextView matrix = new TextView(this);
for (int i = 0; i < size; i++) {
TableRow row = new TableRow(this);
for (int j = 0; j < size; j++) {
cells[i][j] = new EditText(this);
cells[i][j].setInputType(InputType.TYPE_CLASS_NUMBER);
row.addView(cells[i][j]);
}
table.addView(row);
}
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = Integer.parseInt(cells[i][j].getText().toString());
matrix.append(a[i][j] + " ");
}
matrix.append("\n");
}
table.addView(matrix);
setContentView(table);
但我得到一个例外。我知道为什么我会得到它,但我不知道如何避免它
堆栈追踪:
03-19 17:16:59.366 3610-3610/com.example.matrixtest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.matrixtest, PID: 3610
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.matrixtest/com.example.matrixtest.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
Caused by: java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parseInt(Integer.java:358)
at java.lang.Integer.parseInt(Integer.java:334)
at com.example.matrixtest.MainActivity.onCreate(MainActivity.java:33)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
03-19 17:17:02.059 3610-3610/? I/Process: Sending signal. PID: 3610 SIG: 9