使用Random.nextInt()的IllegalArgumentException

时间:2012-09-27 17:10:13

标签: android exception

我写过琐事数学应用程序。它正常启动但在10-20问题之后我得到了消息“应用程序意外停止”。 这是代码。我知道它很长但也许你可以帮忙

package com.example.learn2;


public class Learn extends Activity {
private Button option_1;  
private Button option_2;  
private Button option_3;  
private Button option_4;
private Button next;
private Button question;

private LinearLayout layout1;
private int[] arrayOf4Cell;

private TextView textScore;
private LinearLayout.LayoutParams params;
private int score=0;



///////////////////
private String xString;
private String yString;
private String ques;
private String correctAnswerTHE_ANSWERString;

private String a;
private String b;
private String c;
private String d;

/////////////////
private Random r;
private Random theQuestion;
private Random oneTOfour;


//////////////
private int correctAnswer;
private int correctAnswerTHE_ANSWER;

private int x;
private int y;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

     r= new Random();
     theQuestion= new Random();
     oneTOfour= new Random();

    option_1= new Button(this);
    option_2= new Button(this);
    option_3= new Button(this);
    option_4= new Button(this);
    question= new Button(this);
    next= new Button(this);
    next.setText("שאלה הבאה");

    params = new LinearLayout.LayoutParams(
            500, LayoutParams.WRAP_CONTENT);

    textScore= new TextView(this);
    arrayOf4Cell= new int[4];

    layout1 = new LinearLayout(this);

    option_1.setTextSize(10);
    option_2.setTextSize(10);
    option_3.setTextSize(10);
    option_4.setTextSize(10);

    question.setWidth(10);

    next.setTextSize(20);
    next.setGravity(Gravity.RIGHT);

    option_1.setGravity(Gravity.CENTER);
    option_2.setGravity(Gravity.CENTER);
    option_3.setGravity(Gravity.CENTER);
    option_4.setGravity(Gravity.CENTER);


    question.setGravity((Gravity.CENTER));
    question.setTextSize(20);
    question.setBackgroundColor(Color.CYAN);
    String scoreString=Integer.toString(score);
    textScore.setText(scoreString);






    ////////////////listener for each button
    option_1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String text=(String) option_1.getText();
            if(text.equals(correctAnswerTHE_ANSWERString)){
                Toast.makeText(Learn.this,"נכון!!", Toast.LENGTH_LONG).show();
                score++;
                String scoreString=Integer.toString(score);
                textScore.setText(scoreString);
                createAllLauout2();
            }
            else{
                Toast.makeText(Learn.this,"טעות. נסה שוב.", Toast.LENGTH_LONG).show();
                score--;
                String scoreString=Integer.toString(score);
                textScore.setText(scoreString);

            }

        }
    });

    option_2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String text=(String) option_2.getText();
            if(text.equals(correctAnswerTHE_ANSWERString)){
                Toast.makeText(Learn.this,"נכון!!", Toast.LENGTH_LONG).show();
                score++;
                String scoreString=Integer.toString(score);
                textScore.setText(scoreString);
                createAllLauout2();

            }
            else{
                Toast.makeText(Learn.this,"טעות. נסה שוב.", Toast.LENGTH_LONG).show();
                score--;
                String scoreString=Integer.toString(score);
                textScore.setText(scoreString);
            }

        }
    });

    option_3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String text=(String) option_3.getText();
            if(text.equals(correctAnswerTHE_ANSWERString)){
                Toast.makeText(Learn.this,"נכון!!", Toast.LENGTH_LONG).show();
                score++;
                String scoreString=Integer.toString(score);
                textScore.setText(scoreString);
                createAllLauout2();
            }
            else{
                Toast.makeText(Learn.this,"טעות. נסה שוב.", Toast.LENGTH_LONG).show();
                score--;
                String scoreString=Integer.toString(score);
                textScore.setText(scoreString);
            }

        }
    });

    option_4.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String text=(String) option_4.getText();
            if(text.equals(correctAnswerTHE_ANSWERString)){
                Toast.makeText(Learn.this,"נכון!!", Toast.LENGTH_LONG).show();
                score++;
                String scoreString=Integer.toString(score);
                textScore.setText(scoreString);
                createAllLauout2();
            }
            else{
                Toast.makeText(Learn.this,"טעות. נסה שוב.", Toast.LENGTH_LONG).show();
                score--;
                String scoreString=Integer.toString(score);
                textScore.setText(scoreString);

            }

        }
    });




    layout1.setGravity(Gravity.CENTER_VERTICAL);
    layout1.setOrientation(LinearLayout.VERTICAL);
    layout1.setBackgroundColor(Color.argb(5, 102, 102, 204));
    layout1.addView(textScore);
    layout1.addView(question);
    layout1.addView( option_1);
    layout1.addView(option_2);
    layout1.addView(option_3);
    layout1.addView(option_4);  
    layout1.addView(next); 

    createAllLauout2();




    setContentView(layout1);


    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            createAllLauout2();


        }
    });


}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_learn, menu);
    return true;
}

public void  createAllLauout2(){




    correctAnswer=oneTOfour.nextInt(4);
     correctAnswerTHE_ANSWER = r.nextInt(10);
    correctAnswerTHE_ANSWERString=Integer.toString(correctAnswerTHE_ANSWER);
     x=theQuestion.nextInt(correctAnswerTHE_ANSWER); // x in x+y
     y=correctAnswerTHE_ANSWER-x; //y in x+y

    xString=Integer.toString(x);
    yString=Integer.toString(y);
    ques=xString + "+" + yString;
    question.setText(ques);



    arrayOf4Cell[0]=correctAnswerTHE_ANSWER+3;
    arrayOf4Cell[1]=correctAnswerTHE_ANSWER*6;
    arrayOf4Cell[2]=correctAnswerTHE_ANSWER*2+4;
    arrayOf4Cell[3]=correctAnswerTHE_ANSWER*5+1;

    arrayOf4Cell[correctAnswer]=correctAnswerTHE_ANSWER; //one of the cell get the right answer


     a=Integer.toString(arrayOf4Cell[0]);
     b=Integer.toString(arrayOf4Cell[1]);
     c=Integer.toString(arrayOf4Cell[2]);
     d=Integer.toString(arrayOf4Cell[3]);

    option_1.setText(a);
    option_2.setText(b);
    option_3.setText(c);
    option_4.setText(d);


    layout1.updateViewLayout(question, params);
    layout1.updateViewLayout(option_1, params);
    layout1.updateViewLayout(option_2, params);
    layout1.updateViewLayout(option_3, params);
    layout1.updateViewLayout(option_4, params);
    layout1.updateViewLayout(textScore, params);

}

}

这是logcat:

09-27 16:59:06.142: D/AndroidRuntime(2314): Shutting down VM
09-27 16:59:06.152: W/dalvikvm(2314): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-27 16:59:06.152: E/AndroidRuntime(2314): FATAL EXCEPTION: main
09-27 16:59:06.152: E/AndroidRuntime(2314): java.lang.IllegalArgumentException
09-27 16:59:06.152: E/AndroidRuntime(2314):     at java.util.Random.nextInt(Random.java:225)
09-27 16:59:06.152: E/AndroidRuntime(2314):     at com.example.learn2.Learn.createAllLauout2(Learn.java:246)
09-27 16:59:06.152: E/AndroidRuntime(2314):     at com.example.learn2.Learn$4.onClick(Learn.java:183)
09-27 16:59:06.152: E/AndroidRuntime(2314):     at android.view.View.performClick(View.java:2408)
09-27 16:59:06.152: E/AndroidRuntime(2314):     at android.view.View$PerformClick.run(View.java:8816)
09-27 16:59:06.152: E/AndroidRuntime(2314):     at android.os.Handler.handleCallback(Handler.java:587)
09-27 16:59:06.152: E/AndroidRuntime(2314):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-27 16:59:06.152: E/AndroidRuntime(2314):     at android.os.Looper.loop(Looper.java:123)
09-27 16:59:06.152: E/AndroidRuntime(2314):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-27 16:59:06.152: E/AndroidRuntime(2314):     at java.lang.reflect.Method.invokeNative(Native Method)
09-27 16:59:06.152: E/AndroidRuntime(2314):     at java.lang.reflect.Method.invoke(Method.java:521)
09-27 16:59:06.152: E/AndroidRuntime(2314):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-27 16:59:06.152: E/AndroidRuntime(2314):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-27 16:59:06.152: E/AndroidRuntime(2314):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

问题在于:

x=theQuestion.nextInt(correctAnswerTHE_ANSWER);

correctAnswerTHE_ANSWER0时。

您无法拨打nextInt(0)。查看Random#nextInt()`


快速解决方法是检查:

if(correctAnswerTHE_ANSWER == 0)
    x = 0;
else
    x=theQuestion.nextInt(correctAnswerTHE_ANSWER);

但我不确定你在做什么,所以希望你能找到更好的解决方案。