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

  • Home
  • SEARCH
  • 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 8573001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:09:07+00:00 2026-06-11T19:09:07+00:00

When I click home button onPause method is called but when I call application

  • 0

When I click home button onPause method is called but when I call application in front onResume is called and than it crashes.

There is code when index is out of bouds:

while (isRunning) {
                if (!ourHolder.getSurface().isValid())
                    continue;

                Canvas canvas = ourHolder.lockCanvas();
                canvas.drawRGB(02, 02, 150);
                fingerx = (x - (Gball.getWidth() / 2));
                fingery = (y - (Gball.getHeight() * 2));
                for (int i = 0; i < balls.size(); i++) { // here...
                    ballX[i] = i * 155;
                    canvas.drawText("" + score, 40, 100, paint);
                    canvas.drawBitmap(balls.get(i), ballX[i], ChangingY[i],
                            null);

Error is:

FATAL EXCEPTION: Thread-9140
java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
at com.example.mygame.GFXSurface$GameSurface.run(GFXSurface.java:199)
at java.lang.Thread.run(Thread.java:864)

I know index should be 5 here but if it is than application doesn’t even start

OnResume:

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    ourSurfaceView.resume();
}



public void resume() {
            isRunning = true;
            ourThread = new Thread(this);
            ourThread.start();
        }

Run method:

@Override
        public void run() {

            for (int j = 0; j < 4; j++) {
                balls.add(j, RandomBall());
            }
            Paint paint = new Paint();
            paint.setColor(Color.RED);
            paint.setTextSize(100);
            paint1.setStyle(Style.FILL);
            paint1.setColor(Color.RED);

            while (isRunning) {
                if (!ourHolder.getSurface().isValid())
                    continue;

                Canvas canvas = ourHolder.lockCanvas();
                canvas.drawRGB(02, 02, 150);
                fingerx = (x - (Gball.getWidth() / 2));
                fingery = (y - (Gball.getHeight() * 2));
                for (int i = 0; i < balls.size(); i++) {
                    ballX[i] = i * 155;
                    canvas.drawText("" + score, 40, 100, paint);
                    canvas.drawBitmap(balls.get(i), ballX[i], ChangingY[i],
                            null);
                    if (x != 0 && y != 0 && mode == 0) {
                        if (fingerx < 0) {
                            canvas.drawBitmap(Gball, 0, fingery, null);
                        } else if (fingery < 0) {
                            canvas.drawBitmap(Gball, fingerx, 0, null);
                        }
                        else
                            canvas.drawBitmap(Gball, fingerx, fingery, null);
                        checkForCollision(i);
                    }
                    if (ChangingY[i] <= canvas.getHeight()) {
                        ChangingY[i] += 1 * speed;
                    } else if (ChangingY[i] > canvas.getHeight()) {
                        ChangingY[i] = -72;
                        if (speed <= 11)
                            speed += 0.25;
                        removeAndAdd(this, i);

                    }
                }

                ourHolder.unlockCanvasAndPost(canvas);
            }
        }
  • 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-11T19:09:08+00:00Added an answer on June 11, 2026 at 7:09 pm

    You are looping based on the size of balls but within your loop you are accessing both ballX and ChangingY using the index i. If ballX is smaller than balls then you will get an ArrayIndexOutOfBoundsException.

    Since you are updating the values within the array ballX during the loop, then you probably want to create a new array using the size of balls before the loop starts:

    ballX = new int[balls.size()];
    

    Don’t know what ChangingY is, but you will need to make sure that is also at least as big as balls.

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

Sidebar

Related Questions

click the button to call this method: private void goRegister(final boolean isUsername) { new
When i click home button and then come back to to my application it
Another problem in my android application. When i click on home button, it exits
I want to finish the Activity on Home Button Click . I have below
Hi in my iPhone app if use for sometime and click home button of
I would like to play a sound when someone click home button of iphone/ipad,
When I click the home button on the device. This is a part of
Now i have to click the Home button twice and press one app until
Possible Duplicate: Can i override HOME Button on my own Home-Screen from my application?
I need my activity to handle HOME button press with a receiver programmatically, but

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.