سؤال

In my app, I am trying to get my activity to change text on a button. When I call the ansGen() method in onCreate() , I get a black screen. And after a while, an error comes up and the app closes.

But when the ansGen() method call in onCreate() is commented, it shows up alright (obviously without the functions of the ansGen() method.

Here is my code with the ansGen() method (without imports and onCreateOptionsMenu):

public class QuizActivity extends Activity
{
Button Answer1;
Button Answer2;
Button Answer3;
Button Answer4;

String[] QuestionArray =
{ "What element has atomic number 1?",
        "What is the second most abundant element on Earth?",
        "Element with symbol Li ?", "Has 4 protons?" };
String[] AnswerArray =
{ "Hydrogen", "Helium", "Lithium", "Beryllium", "Boron", "Carbon",
        "Nitrogen" };




public String tempStringy="";

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);

    int AnsArraylen=AnswerArray.length;



    Answer1 = (Button) findViewById(R.id.Answer1);
    Answer2 = (Button) findViewById(R.id.Answer2);
    Answer3 = (Button) findViewById(R.id.Answer3);
    Answer4 = (Button) findViewById(R.id.Answer4);

    //AnswerArray[randAnswers[0]]


    Answer1.setText("Hello");
    Answer2.setText("Hello");
    Answer3.setText("Hello");
    Answer4.setText("Hello");       


    **ansGen(AnsArraylen);**

}


public void ansGen(int AnsArraylen)
{
    Random randomizer=new Random();
    int[] AnsVal={-1,-1,-1,-1};
    int numchecker=0;

    for (int x=0;x<4;x+=1)
    {
        int tempVal=randomizer.nextInt(AnsArraylen);

        if (tempVal==AnsVal[0])
        {
            numchecker=1;
        }
        if (tempVal==AnsVal[1])
        {
            numchecker=1;
        }
        if (tempVal==AnsVal[2])
        {
            numchecker=1;
        }
        if (tempVal==AnsVal[3])
        {
            numchecker=1;
        }


        if (numchecker==0)
        {
            AnsVal[x]=tempVal;
        }

        if (numchecker==1)
        {
            x-=1;
        }

    }

    tempStringy=AnswerArray[AnsVal[0]];
    //Answer1.setText("Hup");
}

I have searched everywhere with no avail.

Thank you for any help!

هل كانت مفيدة؟

المحلول

When you call randomizer.nextInt(AnsArraylen) you can get values between 0 and 3, inclusive. If this tempVal is 0, you are fine, as you just set the AnsArray[x] to the current tempVal. However, what happens when tempVal is 1, for example? You go into none of those ifs, except for the last one, which sets x to -1, so on the next loop iteration it'll become 0 again. Besides whether this makes sense to you, the mistake is in the next line. You are in that case doing:

tempStringy=AnswerArray[AnsVal[0]];

but AnsVal[0] is still -1. You are therefore trying to do:

AnswerArray[-1]

That's where you are getting an Exception. Wrap that in a try-catch and print the stack trace to confirm. Perhaps you wanted to initialize AnsVal as:

int[] AnsVal={0,1,2,3};

نصائح أخرى

I tested your code and find that

if (numchecker==1)
         {
             x-=1;
         }

this condition in your code make the x for loop with value 1 so it will be infinitive loop each time x will be with value 2 this condition make x = 1 so the condition in loop x < 4 will not occur and you will get message with (wait or close program) message

i don't know your logic in code , so try to handler it

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top