在过去的 2-3 个小时里,我一直在进行更改,试图找出我哪里出了问题。我想我试图修复它让情况变得更糟……请你看看并告诉我哪里出了问题? 我要做的是:动态创建editexts(每次按下按钮时2个)。值进入2个列表(每列editexts一个)。从那里它们相互相乘并进入第3个列表应该然后添加所有值并给我总计。
我知道它不完整,但我停止进一步,因为它开始崩溃......这是代码:
List<EditText> allpret = new ArrayList<EditText>();
List<EditText> allcant = new ArrayList<EditText>();
List<Float> alltotal = new ArrayList<Float>();
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);
}
float temp=0;
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; k < allpret.size(); k++) {
pret[k] = allpret.get(k).getText().toString();}
Float[] totaluri = new Float[alltotal.size()];
for(int l=0;l<allpret.size();l++){
Float temp = Float.parseFloat(cant[l]) * Float.parseFloat(pret[l]);
alltotal.add(temp);
totaluri[l] = temp ; }
TextView totalf = (TextView) findViewById(R.id.total);
totalf.setText("Total: " +totaluri[1]); }
这是 logcat :
06-09 04:03:58.230: E/AndroidRuntime(22651): java.lang.IllegalStateException: Could not execute method of the activity
06-09 04:03:58.230: E/AndroidRuntime(22651): at android.view.View$1.onClick(View.java:3704)
06-09 04:03:58.230: E/AndroidRuntime(22651): at android.view.View.performClick(View.java:4232)
06-09 04:03:58.230: E/AndroidRuntime(22651): at android.view.View$PerformClick.run(View.java:17318)
06-09 04:03:58.230: E/AndroidRuntime(22651): at android.os.Handler.handleCallback(Handler.java:615)
06-09 04:03:58.230: E/AndroidRuntime(22651): at android.os.Handler.dispatchMessage(Handler.java:92)
06-09 04:03:58.230: E/AndroidRuntime(22651): at android.os.Looper.loop(Looper.java:137)
06-09 04:03:58.230: E/AndroidRuntime(22651): at android.app.ActivityThread.main(ActivityThread.java:4921)
06-09 04:03:58.230: E/AndroidRuntime(22651): at java.lang.reflect.Method.invokeNative(Native Method)
06-09 04:03:58.230: E/AndroidRuntime(22651): at java.lang.reflect.Method.invoke(Method.java:511)
06-09 04:03:58.230: E/AndroidRuntime(22651): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
06-09 04:03:58.230: E/AndroidRuntime(22651): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
06-09 04:03:58.230: E/AndroidRuntime(22651): at dalvik.system.NativeStart.main(Native Method)
06-09 04:03:58.230: E/AndroidRuntime(22651): Caused by: java.lang.reflect.InvocationTargetException
06-09 04:03:58.230: E/AndroidRuntime(22651): at java.lang.reflect.Method.invokeNative(Native Method)
06-09 04:03:58.230: E/AndroidRuntime(22651): at java.lang.reflect.Method.invoke(Method.java:511)
06-09 04:03:58.230: E/AndroidRuntime(22651): at android.view.View$1.onClick(View.java:3699)
06-09 04:03:58.230: E/AndroidRuntime(22651): ... 11 more
06-09 04:03:58.230: E/AndroidRuntime(22651): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
06-09 04:03:58.230: E/AndroidRuntime(22651): at com.example.testlayout.MainActivity.calculeaza(MainActivity.java:76)
06-09 04:03:58.230: E/AndroidRuntime(22651): ... 14 more
06-09 04:03:58.260: D/dalvikvm(22651): GC_CONCURRENT freed 234K, 11% free 7248K/8135K, paused 2ms+2ms, total 36ms