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 6836713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:23:20+00:00 2026-05-26T23:23:20+00:00

as soon as I use method Game(), my app crashes, why? Also I’d like

  • 0

as soon as I use method Game(), my app crashes, why?
Also I’d like to know if it’s possible to make my code shorter in terms of space it takes & better.

Code:

package com.aleksei.etb;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;

public class ETBetaActivity extends Activity implements View.OnClickListener {

    Button answer_1,
    answer_2,answer_3,
    answer_4,main;

    TextView q_textview,
    TextView tip;

    private String aString[];

    private int i1 = 0;
    private int correct = 0;

    private boolean alive = false;

    MediaPlayer button_click;

    private String[] questions ={"Question 1" , "Question 2","Question 5"};
    private String[] answers_correct ={"Correct answer 1", "Correct answer 2","Correct answer 3","Correct answer 4","Correct answer 5"};

    List<String> question_list = new ArrayList<String>();
    List<String> answer_list_correct = new ArrayList<String>();


    @Override
    public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            getData();
    }

    @Override
    public void onClick(View view) {

        button_click = MediaPlayer.create(this, R.raw.button_click);
        button_click.start();
        switch(view.getId()){

           case R.id.button5: //main
                             if(!alive)
                             alive = true;
                             break;
           case R.id.button1: //answer_1
                             if(alive == false)
                               return;
                             if(correct(1))
                               correct++;           
                             break;
           case R.id.button2: //answer_2
                             if(alive == false)
                               return;
                             if(correct(2))
                               correct++;
                             break;
           case R.id.button3: //answer_3
                             if(alive == false)
                               return;
                             if(correct(3))
                               correct++;           
                             break;
           case R.id.button4: //answer_3
                             if(alive == false)
                               return;
                             if(correct(4))
                               correct++;
                             break;
               default:
                             break;     
           }
           Game();      
    }

    private boolean correct(int button){

       try {
            for (int i = 0; i < answers_correct.length; i++){
                 if(button == 1 && aString[0] == answers_correct[i]
            || button == 2 && aString[1] == answers_correct[i]
            || button == 3 && aString[2] == answers_correct[i]
            || button == 4 && aString[3] == answers_correct[i])
                        return true;
            }
       } 
       catch(Exception ex){
            System.out.println(ex);
       }
       return false;
    }

    private void Game(){

        if(i1 > questions.length) //no more questions
            return;
        main.setText("Next");
            try {
                    String answer_list[][] = {
                               {answers_correct[i1], "Answer 1-2" , "Answer 1-3" , "Answer 1-4"},
                               {answers_correct[i1], "Answer 2-2" , "Answer 2-3" , "Answer 2-4"},
                               {answers_correct[i1], "Answer 3-2" , "Answer 3-3" , "Answer 3-4"},
                               {answers_correct[i1], "Answer 4-2" , "Answer 4-3" , "Answer 4-4"},
                               {answers_correct[i1], "Answer 5-2" , "Answer 5-3" , "Answer 5-4"}};

                    Collections.shuffle(Arrays.asList(answer_list[i1]));
                    answer_1.setText(answer_list[i1][0]);
                    answer_2.setText(answer_list[i1][1]);
                    answer_3.setText(answer_list[i1][2]);
                    answer_4.setText(answer_list[i1][3]);
                    aString[0] = answer_list[i1][0];
                    aString[1] = answer_list[i1][1];
                    aString[2] = answer_list[i1][2];
                    aString[3] = answer_list[i1][3];
                    q_textview.setText(questions[i1]);
          } 
          catch(Exception e){
            System.out.println(e);
          }
          tip.setText(correct);

        /*questions = question_list.toArray(new String[question_list.size()]);
         answers_correct = answer_list_correct.toArray(new                  String[answer_list_correct.size()]);

        question.setText(questions[i1]);        

        answer_list_correct.remove(questions[i1]);
        question_list.remove(questions[i1]);*/
         i1++;
    }
    private void getData(){
        //Getting the data
        main = (Button) findViewById(R.id.button5);
        answer_1 = (Button) findViewById(R.id.button1);
        answer_2 = (Button) findViewById(R.id.button2);
        answer_3 = (Button) findViewById(R.id.button3);
        answer_4 = (Button) findViewById(R.id.button4);
        q_textview = (TextView) findViewById(R.id.question);
        tip = (TextView) findViewById(R.id.answ1);

        //Making the buttons, actually work
        main.setOnClickListener(this);
        answer_1.setOnClickListener(this);
        answer_2.setOnClickListener(this);
        answer_3.setOnClickListener(this);
        answer_4.setOnClickListener(this);

            //Resets the text
            //Note to self: Replace with another ContectView
        main.setText("Begin!");
        answer_4.setText("");
        answer_3.setText("");
        answer_2.setText("");
        answer_1.setText("");

    /*  for(String x : questions) {
            for(String y : answers_correct){

            answer_list_correct.add(y);
            question_list.add(x);

            Collections.shuffle(answer_list_correct);
            Collections.shuffle(question_list);

            }
        } */
    }
}

Best regards.

  • 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-05-26T23:23:20+00:00Added an answer on May 26, 2026 at 11:23 pm

    In Game(), you check i1 > questions.length at the top, but try q_textview.setText(questions[i1]); near the bottom. If i1 == questions.length, that would throw an ArrayIndexOutOfBoundsException (hope I remembered the name correctly), make it i1 >= questions.length. Also, you use i1 as index for answers_correct and answer_list, that won’t be out of bounds for the pasted questions and answers_correct, but might be for the real (unlikely, though, you won’t have more questions than answers, would you?).

    In correct, your if statement

    if(button == 1 && aString[0] == answers_correct[i]
        || button == 2 && aString[1] == answers_correct[i]
        || button == 3 && aString[2] == answers_correct[i]
        || button == 4 && aString[3] == answers_correct[i])
        return true;
    

    could be shortened to

    if (aString[button-1] == answers_correct[i]) return true;
    

    (unless button can have values < 1 or > 4, then you would need to check for button >= 1 && button <= 4 too). However, using == to compare Strings is dangerous and almost certainly wrong. == compares the equality of references, so string1 == string2 is true only if both refer to the same String instance. If, as seems to be the case, all your Strings come from string literals in the source code, it will kind of work because then there is only one instance for each literal, but if outside Strings could enter the game, you must use equals to compare Strings. Use equals also in those cases where == would (sort of accidentally) work.

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

Sidebar

Related Questions

Using jQuery , I can use the following function to execute code as soon
In keeping with the DRY-principle I try to use partials as soon as I
I'll soon be posting an article on my blog , but I'd like to
I'm soon going to be starting some iPhone Development (3.0) building a simple app
I will soon need to add SSO to an ASP.NET app using SAML. The
I use the code below to insert data into my sqlite-database but for some
I've written a game of tic-tac-toe in Java, and my current method of determining
What is the use of ConvertBack method in the IValueConverter interface. When will it
I'm writing this application which is a flashcard app, I would like to create
My app is my website so I used to put this code in my

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.