Monday, April 4, 2016

Black screen and app shutdown when trying to go back to the previous activity/view

When i run my script below everything works fine. When i want to go back to the previous activity i get a black screen on the emulator and then the app shuts down, i also get a black screen when i exit the application and try to resume it.

The script:

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 the with my gameloop Thread i think. Unfortunately i dont see what the problem is in my code... Can anyone help me with this?


from xda-developers http://ift.tt/1V4kW6U
via IFTTT

No comments:

Post a Comment