0

When I run my code below everything works fine. When I want to go back to the previous activity (using the emulator) I get a black screen and then the app shuts down. I also get a black screen when I exit the application and try to resume it.

The code:

package com.example.bono.as3;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
import java.io.InputStream;

public class Main extends Activity {

    DrawView drawView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        drawView = new DrawView(this);
        setContentView(drawView);
    }

    @Override public void onResume(){
        super.onResume();
        drawView.resume();
    }

    @Override public void onPause(){
        super.onPause();
        drawView.pause();
    }

    public class DrawView extends SurfaceView implements Runnable{

        Thread gameloop = new Thread();
        SurfaceHolder surface;
        volatile boolean running = false;
        AssetManager assets = null;
        BitmapFactory.Options options = null;
        Bitmap incect[];
        int frame = 0;

        public DrawView(Context context){
            super(context);
            surface = getHolder();
            assets = context.getAssets();
            options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;

            incect = new Bitmap[2];

            try {
                for (int n = 0; n < incect.length; n++){
                    String filename = "incect"+Integer.toString(n+1)+".png";
                    InputStream istream = assets.open(filename);
                    incect[n] = BitmapFactory.decodeStream(istream,null,options);
                    istream.close();
                }
            } catch (IOException e){
                e.printStackTrace();
            }
        }

        public void resume(){
            running = true;
            gameloop = new Thread(this);
            gameloop.start();
        }

        public void pause(){
            running = false;
            while (true){
                try {
                    gameloop.join();
                }
                catch (InterruptedException e){}
            }
        }

        @Override public void run (){
            while (running){
                if (!surface.getSurface().isValid())
                    continue;
                Canvas canvas = surface.lockCanvas();
                canvas.drawColor(Color.rgb(85, 107, 47));
                canvas.drawBitmap(incect[frame], 0, 0, null);

                surface.unlockCanvasAndPost(canvas);

                frame++;

                if (frame > 1) frame = 0;

                try {
                    Thread.sleep(500);
                } catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }
    }
}

I dont get any error message in the log, what I do get is about 13 messages saying "suspending all threads took: X ms" so it has something to do with my gameloop Thread I think. Unfortunately I don't see what the problem is in my code...

Levon
  • 1,681
  • 2
  • 18
  • 40
Taegos
  • 343
  • 3
  • 6
  • 15
  • can anyone help me with this issue? – Taegos Apr 04 '16 at 12:09
  • Firstly, in `pause` when you catch `InterruptedException`, print stack trace out, don't just catch it and do nothing. It's dangerous. Second, when pause, you let the `gameloop` wait, but `onResume`, you create another new `thread` without handling the thread you put on wait. So I guess after sometimes of pause and resume, you would have lot of thread hanging around waiting. – Nguyen Quang Anh Apr 06 '16 at 07:16

2 Answers2

1

This is what i use to navigate back. Maybe this is usefull for you 2!

Button backpressed = (Button) findViewById(R.id.btBack);
    backpressed.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            onBackPressed();
        }
    });

The onBackPressed() is just a method that you can call if you want to return to the last page.

What it looks like for me:

Backbutton

Robbert
  • 68
  • 2
  • 12
  • Thank you for the answer. On all my other projects i dont need to write additional code to navigate back so i dont understand why i would need it now. Anyway, i will try out your solution. – Taegos Apr 04 '16 at 14:40
  • @Taegos i hope it works, i am relatively new to android, so i dont know if it works in every android version. – Robbert Apr 04 '16 at 14:48
  • @Taegos Did my awnser help you? – Robbert Apr 05 '16 at 12:38
  • I did not really understand it, why do you add a new button in your app to go back to a class or view? What i am using in all my projects is the default back button that is a part of the android GUI that does not need additional coding. On older devices this is usually a physical button, do you now what i am referring to? – Taegos Apr 05 '16 at 15:53
  • @Taegos i have a button <-- in my toolbar on top of the screen, when pressed it activates the onBackpressed() method. Thats what this awnser is about, wich i explained under the code. aka the onBackPressed method can be used in many different ways and doesnt have to be in a button, but can be behind a line of code for example. Hope this helps your awnser. I edited in a photo to make you understand more of my awnser. – Robbert Apr 06 '16 at 07:05
1

If you need a context, use getApplicationContext() or Activity.this. (seems less buggy with my code)

While going back to a previously used activity, onCreate() is not called, only onResume(), so it might help if you move the following lines to there. drawView = new DrawView(this); setContentView(drawView);

  • Why use inner class at all? Activity is a class already. If you want to use the code multiple times, make it a class on its own.