Sorin Grecu
Sorin Grecu

Reputation: 1034

App crashes,why is that?

So,i want to make this app that puts the EditTexts that i create dynamically in a list and then reads from that list but i'm kind of stuck,my app crashes.Could you please tell me what i'm doing wrong ?I've tested different things,tried to change some stuff but i didn't find the problem. I tried to see if it can show cant[1] but it crashes even when trying that. Here's my code :

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

    }

}

And here's 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

Upvotes: 0

Views: 73

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007544

Your inner for loop -- the one attempting to use k as the index -- will try to loop forever, as you are comparing j against allpret.size() instead of k. Try:

    for (int k = 0; k < allpret.size(); k++) {
        pret[k] = allpret.get(k).getText().toString();
    }

Upvotes: 3

Related Questions