Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8594171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:08:25+00:00 2026-06-12T00:08:25+00:00

I wrote trivia math application. It is start normally but after 10-20 question i

  • 0

I wrote trivia math application. It is start normally but after 10-20 question i got the mesage “the application has stopped unexpectedly”.
This is the code. I know it is long but maybe you can help

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

}

}

This is 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:08:26+00:00Added an answer on June 12, 2026 at 12:08 am

    The problem is here:

    x=theQuestion.nextInt(correctAnswerTHE_ANSWER);
    

    when correctAnswerTHE_ANSWER is 0.

    You cannot call nextInt(0). Check out Random#nextInt()`.


    A quick fix is to check:

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

    But I’m not certain what you are doing so hopefully you will find a better solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a single console application (just a part of a site code, but
this is a general question about GUIs. I never wrote a program with GUI,
I wrote a simple XML file and a DTD file including an entity, but
Not specifically a programming question, but might be something a bunch of programmers run
I wrote this simple Prolog program. man(socrates). mortal(X) :- man(X). immortal(X) :- immortal(X). I
I'm not sure if this is a trivial questions but in a PHP class:
I wrote a parser for a non-trivial language this weekend. Some of the output
I have a simple (and also trivial) banking application that I wrote in C++.
I wrote a trivia game in Flex (flash). The site is written entirely in
May be it's a silly (or more than trivial) kinda question, but it seems

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.