lundi 3 avril 2017

Not able to generate Random images some times in android

I'm trying to generate random images several times from an array in an android app. but Those images are generated one time that are skip and try to store remaining images in new array and select again a new random image. But some time my app crashes due to "IllegalArgumentException: n <= 0: 0" So how to fix this issue?

Here is my code:

  all = new int[]{R.id.img3_1, R.id.img3_2, R.id.img3_3, R.id.img3_4,R.id.img3_5, R.id.img3_6, R.id.img3_7, R.id.img3_8,
                R.id.img3_9,R.id.img3_10,R.id.img3_11,R.id.img3_12,R.id.img3_13,R.id.img3_14,R.id.img3_15,R.id.img3_16};

        id = all[random.nextInt(all.length)];
        ObjectAnimator animator = ObjectAnimator.ofInt(findViewById(id), "backgroundResource", R.drawable.new_stateimg, R.drawable.org_state).setDuration(2000);
        animator.setEvaluator(new ArgbEvaluator());
        animator.start();


        final int index = Ints.indexOf(all, id);
        all2 = Arrays.copyOf(all, index);
        id1 = all2[random.nextInt(all2.length)];

        ObjectAnimator animator1 = ObjectAnimator.ofInt(findViewById(id1), "backgroundResource", R.drawable.new_stateimg, R.drawable.org_state).setDuration(2000);
        animator1.setEvaluator(new ArgbEvaluator());
        animator1.start();


        final int index1 = Ints.indexOf(all2, id1);
        int[] all3 = Arrays.copyOf(all2, index1);
        id2 = all3[random.nextInt(all3.length)];//line 271


        ObjectAnimator animator2 = ObjectAnimator.ofInt(findViewById(id2), "backgroundResource", R.drawable.new_stateimg, R.drawable.org_state).setDuration(2000);
        animator2.setEvaluator(new ArgbEvaluator());
        animator2.start();

my Log:

FATAL EXCEPTION: main
Process: brainreadersystem.com.ncss, PID: 7474
java.lang.IllegalArgumentException: n <= 0: 0
    at java.util.Random.nextInt(Random.java:182)
    at brainreadersystem.com.ncss.Game.linear16(Game.java:271)
    at brainreadersystem.com.ncss.Game.check(Game.java:236)
    at brainreadersystem.com.ncss.Game.access$000(Game.java:20)
    at brainreadersystem.com.ncss.Game$4.onClick(Game.java:184)
    at android.view.View.performClick(View.java:4848)
    at android.view.View$PerformClick.run(View.java:20262)
    at android.os.Handler.handleCallback(Handler.java:815)
    at android.os.Handler.dispatchMessage(Handler.java:104)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5637)
    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:960)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)




Aucun commentaire:

Enregistrer un commentaire