Sign in to follow this  
LAURENT*

Everytime I exit my app and return.....

Recommended Posts

Everytime I exit my app and return it give me an error. The error is "Your game has stopped working", after this I need to tap my app's icon again to reactive it. Most apps allow the user to resume their session after exiting. I want to do the same. I'm new to java and android and have no idea where this error might be. does anyone have a club what I must do?

 

I'm running my apps on my phone because my laptop isn't strong enough.

 

I followed this guy's tutorial BTW

https://www.youtube.com/watch?v=rJcm5Oyi3YA&list=PLWweaDaGRHjvQlpLV0yZDmRKVBdy6rSlg

Share this post


Link to post
Share on other sites

If your app uses OpenGL then try looking into setPreserveEGLContextOnPause. Without it, when your app moves into the background, the OpenGL context and all associated objects are trashed and must be recreated.

Edited by C0lumbo

Share this post


Link to post
Share on other sites

2 suggestions:

 

1. Try debugging it.

2. If that fails, check logcat.

I found the log cat. It was consistence until I did my error process. Here a snap shot of the log cat errors.

http://i.imgur.com/y1FaTbb.png

 

There isn't a lot of code. Maybe, possibly, if it is not too much trouble, look into your soul and check the code?

Share this post


Link to post
Share on other sites

The only thing it leads me to is this. What wrong with the code? I copied it perfectly.

 

 

 

 

The item in blue within the sea of red is clickable. If you click on that that it will take you to the line of code this causing the crash.


@Override
        public void surfaceCreated(SurfaceHolder holder){

            bg = new BackGround(BitmapFactory.decodeResource(getResources(), R.drawable.grassbg1));
            player = new Player(BitmapFactory.decodeResource(getResources(),R.drawable.helicopter), 100, 86, 3 );

            /*/NO USAGE
            smoke = new ArrayList<Smokepuff>();
            smokeStartTime = System.nanoTime();*/


            //cystalPoint = new CystalPoint(BitmapFactory.decodeResource(getResources(),R.drawable.redcyrstal), 91, 86, 1);
            cystalPoint = new ArrayList<CystalPoint>();
            cystalPoint.add(new CystalPoint(BitmapFactory.decodeResource(getResources(),R.drawable.redcyrstal), 91, 86, 1));

            //Safely start game loop
            thread.setRunning(true);
            thread.start();
        }

Share this post


Link to post
Share on other sites

Read the error message. Note where it says "Thread already started" at "thread.start()". Fix that. Don't start a thread that is already started.

Okay I think I understand however I'm still lost. I have this code. When I use something called "try" I set the game's bool variable named "running" to false and that should be enough.

@Override 
public void surfaceDestroyed(SurfaceHolder holder) {
            //Safely end the program

            boolean retry = true;
            int counter = 0;
            while (retry == true && counter < 1000) {

                counter++;
                try {
                    thread.setRunning(false);
                    thread.join();
                    retry = false;
                } catch (InterruptedException e) { e.printStackTrace();}

            }
        }
 public void setRunning(boolean b)
    {
        running = b;
    }
@Override
    public void run() {
        long startTime;
        long timeMillis;
        long waitTime;
        long totalTime = 0;
        int frameCount = 0;
        long targetTime = 1000 / FPS;


        while (running == true) {
            startTime = System.nanoTime();
            canvas = null;


            //Try locking the canvas for pixel editing
            try {
                canvas = this.surfaceHolder.lockCanvas();

                synchronized (surfaceHolder) {

                    this.gamePanel.update();
                    this.gamePanel.draw(canvas);
                }
            } catch (Exception e) {}


            finally {
                if (canvas != null){
                    try {
                        surfaceHolder.unlockCanvasAndPost(canvas);
                    }catch (Exception e){e.printStackTrace();}
                }

            }

            timeMillis = (System.nanoTime() - startTime) / 1000000;
            waitTime = targetTime - timeMillis;

            try {
                this.sleep(waitTime);
            } catch (Exception e) {}

            totalTime += System.nanoTime() - startTime;
            frameCount++;

            if (frameCount == 30) {
                averageFPS = 1000/ ((totalTime/frameCount) / 1000000);
                frameCount = 0;
                totalTime = 0;

                System.out.println(averageFPS);
            }
        }
    }
Edited by LAURENT*

Share this post


Link to post
Share on other sites

After 2 nights of failure I'm back. Please help.

 

 

Read the error message. Note where it says "Thread already started" at "thread.start()". Fix that. Don't start a thread that is already started.

 

I tried but nothing is working. I tried making a Boolean variable and setting it to false and then setting it to true right after the "thread.start" function. The app doesn't give me the error anymore, it gives me a blank black screen, I have to restart the app to get it to work. I tried in various ways.....

 

Unlike c++ there was a lot of stuff automatically done for me that I just accepted but I'm really having a hard time figuring out where this program starts. I know if I could just get a clue how to look at the beginning I know I could solve this.

@Override
        public void surfaceCreated(SurfaceHolder holder){

            if (threadStarted == false) {
            bg = new BackGround(BitmapFactory.decodeResource(getResources(), R.drawable.grassbg1));
            player = new Player(BitmapFactory.decodeResource(getResources(), R.drawable.helicopter), 100, 86, 3);

            /*/NO USAGE
            smoke = new ArrayList<Smokepuff>();
            smokeStartTime = System.nanoTime();*/


            //cystalPoint = new CystalPoint(BitmapFactory.decodeResource(getResources(),R.drawable.redcyrstal), 91, 86, 1);
            cystalPoint = new ArrayList<CystalPoint>();
            cystalPoint.add(new CystalPoint(BitmapFactory.decodeResource(getResources(), R.drawable.redcyrstal), 91, 86, 1));
            cystalPoint.add(new CystalPoint(BitmapFactory.decodeResource(getResources(), R.drawable.redcyrstal), 91, 86, 1));

            //Safely start game loop
            thread.setRunning(true);
           // thread.threadHasBeenMade(true);

                threadStarted = true;
                thread.start();
            }
        }
Edited by LAURENT*

Share this post


Link to post
Share on other sites

Consider adding a few of these at suspicious places:

System.out.println("Thread state = " + thread.getState());

Also, only start the thread if the thread is new and never run before:

if (thread.getState() == Thread.State.NEW)
{
   thread.start();
}
else
{
   System.out.println("Tried to start the thread that was already started.  This is a bug.  Current thread state = " + thread.getState());
}

Share this post


Link to post
Share on other sites

The app does the same thing. It might not give me the "The app has stopped working" error anymore but now it's just a black screen. I'm still not getting anywhere and I'm starting to regret picking this tutorial. Since I don't have the competence yet to pick apart this problem and present enough details I'm giving out everything.

 

 

In this link is a download of the project. It is not a big project, it is very small and only used for learning. I made it for android studios.  please take a look and correct the error.

https://www.dropbox.com/home?preview=MyFirstGame.zip

 

I will continue my obsessive research. I don't think I can present good enough details anymore to receive good help, so here is everything. Happy thanksgiving everyone btw.

Share this post


Link to post
Share on other sites

If I understand your problem correctly (I'm not sure I do really) then I do have similar problems a few times.

If you can't track it down with normal debugger then track it down the ugly way, its a dirty hack but it always work whenever I encounter such problems

 

In fact i'm about to fix a similar problem (app stopped working crash) right now the way I described below, and there is no panic on my side cus it always get fixed(adding this line to give you confidence it will work, so don't despair)

 

Strip the code down to bare empty function by commenting every part out

At this stage you won't experience the crash again

Then start uncommenting the lines following the logic of the  code

Run after each comment removed

you should be able to note which line causes the black screen/crash

then you can investigate seperately why the line of code return black screen or crash

                but its very important you follow the logic of the code otherwise you get a bigger crash

Share this post


Link to post
Share on other sites

If I understand your problem correctly (I'm not sure I do really) then I do have similar problems a few times.

If you can't track it down with normal debugger then track it down the ugly way, its a dirty hack but it always work whenever I encounter such problems

 

In fact i'm about to fix a similar problem (app stopped working crash) right now the way I described below, and there is no panic on my side cus it always get fixed(adding this line to give you confidence it will work, so don't despair)

 

Strip the code down to bare empty function by commenting every part out

At this stage you won't experience the crash again

Then start uncommenting the lines following the logic of the  code

Run after each comment removed

you should be able to note which line causes the black screen/crash

then you can investigate seperately why the line of code return black screen or crash

                but its very important you follow the logic of the code otherwise you get a bigger crash

 

Thank you for your words of encouragement however there are core things about this development environment that I come to term as not understanding.

 

*I can not alter my thread class directly which is bothering me

*I do not fully understand why I should start a thread.

*I do not know of all the threads class functions or functionality

*I do not know where the code begins or the hierarchy

*I do not know the GUI of android studio well enough to comprehend all it information

 

I simply followed a tutorial to a tee. I've identity my flaws and will work ever harder to eliminate them. This is a journey I will be taking alone. However if someone want to give me a shortcut and fix the problem that would be nice. I will viciously learn as much as can and return with the solution as a contribution to the site. I must be able to hold my own and be independent also I consider this a low ranking problem that should take no less than 1 page to solve and I will make sure of that!

 

Again thank you for the encouragement and advice.

Share this post


Link to post
Share on other sites

Okay so this was the problem. In android studios you can't start a thread twice and once it is done it will do nothing more or at least that what I've come to conclude. Another problem I face was the automatic garbage collection and being a c++ coder first left me unsatisfied with some magic behind the scenes cleaning up after me.

 

So this is how you solve the problem. Don't let the main thread end and let the automatic garbag collection do it's job. I added a new piece to my code.

 

bg = new BackGround(BitmapFactory.decodeResource(getResources(), R.drawable.grassbg1));
player = new Player(BitmapFactory.decodeResource(getResources(), R.drawable.helicopter), 100, 86, 3);

/*/NO USAGE
smoke = new ArrayList<Smokepuff>();
smokeStartTime = System.nanoTime();*/


//cystalPoint = new CystalPoint(BitmapFactory.decodeResource(getResources(),R.drawable.redcyrstal), 91, 86, 1);
cystalPoint = new ArrayList<CystalPoint>();
cystalPoint.add(new CystalPoint(BitmapFactory.decodeResource(getResources(), R.drawable.redcyrstal), 91, 86, 1));
cystalPoint.add(new CystalPoint(BitmapFactory.decodeResource(getResources(), R.drawable.redcyrstal), 91, 86, 1));

//THIS IS THE PIECE THAT WAS ADDED
thread = new MainThread(getHolder(), this);

 

//Safely start game loop
thread.setRunning(true);
thread.start();

 

and then inside of the function used to deactivate and allocate the thread is set to null for garbage collection purposes. This allows the app to start up fresh and new each time.

@Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            //Safely end the program

            boolean retry = true;
            int counter = 0;
            while (retry == true && counter < 1000) {

                counter++;
                try {
                    thread.setRunning(false);
                    thread.join();
                    retry = false;
?                    //NEW PIECE ADDED
                    thread = null;
                } catch (InterruptedException e) { e.printStackTrace();}

            }
        }

In order to do more fancy stuff I need to learn about threads. From what I can tell, they're used for optimization. Threads don't sound very nice to tackle honestly but I'm filled with determination. I heard about this once from a friend who said this was important so I guess now I have an excuse to start up thread research 8 months early.

 

 

THREAD COMPLETED!

Edited by LAURENT*

Share this post


Link to post
Share on other sites
You could have caught that by copy/pasting the little code block and the statement about only running threads that have never been started, but your research method also works. :-)

Threads aren't really an optimization. Generally they are a way to do one piece of work while another piece of work is blocking execution. Generally the blocking tasks tend to be I/O, and you want to do some processing on your multi-gigahertz machine while waiting milliseconds or longer for data to arrive. There are also some really neat algorithms that take advantage of spreading work between processors, but that's less commonly used.

Threads are powerful but dangerous. Do things well and it can make your program more responsive. However, minor defects can lead to nasty bugs. Instead of one execution path you can have bugs that exist from the interplay of multiple execution paths. Synchronization and race condition issues are among the most difficult to track down.

Share this post


Link to post
Share on other sites

I'm glad it's working, but what you're doing now is throwing away whatever the original thread was, and replacing it with a new one. So there are probably still some flaws with your code. Perhaps the order that Android calls your functions has changed since that tutorial was written. Or perhaps you have a bug somewhere else.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this