所以,我想制作这个应用程序,将我动态创建的 EditTexts 放在一个列表中,然后从该列表中读取,但我有点卡住了,我的应用程序崩溃了。你能告诉我我做错了什么吗?已经测试了不同的东西,试图改变一些东西,但我没有发现问题。我试图看看它是否可以显示 cant[1] 但即使尝试它也会崩溃。这是我的代码:
List<EditText> allpret = new ArrayList<EditText>();
List<EditText> allcant = new ArrayList<EditText>();
public void produsnou(View v) {
LinearLayout l1 = (LinearLayout) findViewById(R.id.layout1);
EditText et = new EditText(this);
et.setHint("Produs");
l1.addView(et);
LinearLayout l2 = (LinearLayout) findViewById(R.id.layout2);
EditText et2 = new EditText(this);
et2.setHint("Cantitate");
et2.setInputType(InputType.TYPE_CLASS_NUMBER
| InputType.TYPE_NUMBER_FLAG_DECIMAL);
allcant.add(et2);
l2.addView(et2);
LinearLayout l3 = (LinearLayout) findViewById(R.id.layout3);
EditText et3 = new EditText(this);
et3.setHint("Pret");
et3.setInputType(InputType.TYPE_CLASS_NUMBER
| InputType.TYPE_NUMBER_FLAG_DECIMAL);
l3.addView(et3);
allpret.add(et3);
}
public void calculeaza(View v) {
String[] cant = new String[allcant.size()];
for (int j = 0; j < allcant.size(); j++) {
cant[j] = allcant.get(j).getText().toString();
String[] pret = new String[allcant.size()];
for (int k = 0; j < allpret.size(); k++) {
pret[k] = allpret.get(k).getText().toString();
}
TextView totalf = (TextView) findViewById(R.id.total);
totalf.setText("Total: " +cant[0]);
}
}
这是logcat:
06-08 23:58:07.655: E/AndroidRuntime(7728): FATAL EXCEPTION: main
06-08 23:58:07.655: E/AndroidRuntime(7728): java.lang.IllegalStateException: Could not execute method of the activity
06-08 23:58:07.655: E/AndroidRuntime(7728): at android.view.View$1.onClick(View.java:3704)
06-08 23:58:07.655: E/AndroidRuntime(7728): at android.view.View.performClick(View.java:4232)
06-08 23:58:07.655: E/AndroidRuntime(7728): at android.view.View$PerformClick.run(View.java:17318)
06-08 23:58:07.655: E/AndroidRuntime(7728): at android.os.Handler.handleCallback(Handler.java:615)
06-08 23:58:07.655: E/AndroidRuntime(7728): at android.os.Handler.dispatchMessage(Handler.java:92)
06-08 23:58:07.655: E/AndroidRuntime(7728): at android.os.Looper.loop(Looper.java:137)
06-08 23:58:07.655: E/AndroidRuntime(7728): at android.app.ActivityThread.main(ActivityThread.java:4921)
06-08 23:58:07.655: E/AndroidRuntime(7728): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 23:58:07.655: E/AndroidRuntime(7728): at java.lang.reflect.Method.invoke(Method.java:511)
06-08 23:58:07.655: E/AndroidRuntime(7728): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
06-08 23:58:07.655: E/AndroidRuntime(7728): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
06-08 23:58:07.655: E/AndroidRuntime(7728): at dalvik.system.NativeStart.main(Native Method)
06-08 23:58:07.655: E/AndroidRuntime(7728): Caused by: java.lang.reflect.InvocationTargetException
06-08 23:58:07.655: E/AndroidRuntime(7728): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 23:58:07.655: E/AndroidRuntime(7728): at java.lang.reflect.Method.invoke(Method.java:511)
06-08 23:58:07.655: E/AndroidRuntime(7728): at android.view.View$1.onClick(View.java:3699)
06-08 23:58:07.655: E/AndroidRuntime(7728): ... 11 more
06-08 23:58:07.655: E/AndroidRuntime(7728): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
06-08 23:58:07.655: E/AndroidRuntime(7728): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
06-08 23:58:07.655: E/AndroidRuntime(7728): at java.util.ArrayList.get(ArrayList.java:304)
06-08 23:58:07.655: E/AndroidRuntime(7728): at com.example.testlayout.MainActivity.calculeaza(MainActivity.java:66)
06-08 23:58:07.655: E/AndroidRuntime(7728): ... 14 more
06-08 23:58:07.680: D/dalvikvm(7728): GC_CONCURRENT freed 237K, 11% free 7249K/8135K, paused 2ms+3ms, total 38ms
06-08 23:58:17.125: I/Process(7728): Sending signal. PID: 7728 SIG: 9